This is a migrated thread and some comments may be shown as answers.

DetailsView Template nested inside a RadGrid

1 Answer 280 Views
Grid
This is a migrated thread and some comments may be shown as answers.
lucian ticrea
Top achievements
Rank 1
lucian ticrea asked on 08 Sep 2009, 03:45 PM
Hello,
I am having problem with maintaing state on the detailsview nested inside the radgrid's NestedViewTemplate.

Here is the aspx version
<telerik:RadGrid VirtualItemCount="1000" ID="RadGridAll"
             AllowPaging="True" AllowSorting="True"
            AllowMultiRowSelection="True" runat="server"
             DataSourceID="SqlDataSource1" GridLines="None" OnItemCommand="RadGridAll_ItemCommand">
            <MasterTableView   AllowCustomPaging="True" DataSourceID="SqlDataSource1" GridLines="Both">
                <Columns>
                    
                    <telerik:GridTemplateColumn UniqueName="CheckBoxTemplate">
                        <HeaderTemplate>
                            <asp:CheckBox ID="headerChkbox" AutoPostBack="true" runat="server" />                            
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="checkbox1"   AutoPostBack="true" runat="server" />
                        </ItemTemplate>
                        <HeaderStyle Width="30px" />
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn UniqueName="TemplateColumn7" DataField="nameLabels" HeaderText="Labels">
                        <HeaderStyle Width="60px" CssClass="rgHeader last"></HeaderStyle>
                        <ItemTemplate>
                                <%# DataBinder.Eval(Container.DataItem,"name") %>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
                <NestedViewTemplate>
                    <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px">
                            <Fields>
                               
                            </Fields>
                    </asp:DetailsView>
              
                </NestedViewTemplate>
             
                <ExpandCollapseColumn Visible="True">
                </ExpandCollapseColumn>
                
            </MasterTableView>
            <FilterMenu EnableEmbeddedSkins="False">
            </FilterMenu>
            <HeaderContextMenu EnableEmbeddedSkins="False">
            </HeaderContextMenu>
            
        </telerik:RadGrid>

and in the code behind  I am adding custom template fields to the detailsview  whenever the user presses the expand button it:

private void AddField(DetailsView dvItemDetails,string column, string header)
    {
        object value = "ddddd";
        TemplateField field = new TemplateField();
        field.ShowHeader = true;
        field.HeaderTemplate = new DetailsViewTemplate(DataControlRowType.Header, header, "", "");
        field.ItemTemplate = new DetailsViewTemplate(DataControlRowType.DataRow, "", column, value);
        dvItemDetails.Fields.Add(field);
    }
    public void CreateColumns(DetailsView dvItemDetails)
    {
            AddField(dvItemDetails,"one","One");
            AddField(dvItemDetails,"two", "Two");
            List<int> source=new List<int>(2);
            source.Add(1);
            source.Add(2);
            dvItemDetails.DataSource = source;
            dvItemDetails.DataBind();
    }
    protected void RadGridAll_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "ExpandCollapse")
        {
            GridDataItem item = RadGridAll.Items[e.Item.ItemIndex];
            GridNestedViewItem template = item.ChildItem;
            DetailsView dvItemDetails = template.FindControl("DetailsView1") as DetailsView;
            CreateColumns(dvItemDetails);
        }
    }

 I am using a custom template class ,DetailsViewTemplate
public class DetailsViewTemplate : ITemplate
{
    private DataControlRowType templateType;
    private string columnHeader;
    private string columnNameBinding;
    private string text = "";

    public DetailsViewTemplate(DataControlRowType type, string colHeader, string colNameBinding, object value)
    {
        templateType = type;
        columnHeader = colHeader;
        columnNameBinding = colNameBinding;
        text = value.ToString();
    }

    public void InstantiateIn(System.Web.UI.Control container)
    {
        switch (templateType)
        {
            case DataControlRowType.Header:
                Literal lc = new Literal();
                lc.Text = columnHeader;
                container.Controls.Add(lc);
                break;
            case DataControlRowType.DataRow:
                Label cb = new Label();
                cb.ID = "cb1";
                cb.Text = text;// GetStudent("select * from ppaSearchColumn").Tables[0].Rows[rowIndex][columnNameBinding].ToString();
                container.Controls.Add(cb);
                break;
            default:
                break;
        }
    }
}
when i expand a row it is presented ok, but when I expand another one, the first one loses the text from the detailsview.
I am new to telerik so please bear with me if this is an easy one.
Anybody has a suggestion?
Thanks,
 Lucian




1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 11 Sep 2009, 10:38 AM
Hello lucian,

Calling databind directly for the control would break its functionalities.
You can use a datasource control, to populate the control with data, as shown in the following example:

http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/nestedviewtemplatedeclarativerelations/defaultcs.aspx

I hope this gets you started properly.

Best wishes,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
lucian ticrea
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or