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

Programmatically add a column to an existing detail table in hierarchy

1 Answer 92 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Donald
Top achievements
Rank 1
Donald asked on 26 Jan 2012, 11:41 AM
Hi,

I tried this in the DetailTableDataBind event handler

GridBoundColumn b = new GridBoundColumn();
e.DetailTableView.Columns.Add(b);
b.HeaderText = "test";

the column is successfully added, but as soon as a postback occurs, the new column disappears. But adding a column the exact same way to the MasterTableView works as intended.

I have also tried this:

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        if (item.Expanded)
        {
           dynamic data = new[] {
            new { ID = "1", Name ="Name11",ParentID = "0"},
            new { ID = "2", Name ="Name11",ParentID = "1"},
            new { ID = "3", Name ="Name11",ParentID = "2"},
            new { ID = "4", Name ="Name11",ParentID = "3"}
              };
  
       
  
            GridBoundColumn b = new GridBoundColumn();
            b.HeaderText = "test";
            b.DataField = "Name";
            item.ChildItem.NestedTableViews[0].Columns.Add(b);
            item.ChildItem.NestedTableViews[0].DataSource = data;
            item.ChildItem.NestedTableViews[0].DataBind();
        }
  
    }
}

Doing this works if I need to programmatically add columns to the first level of the hierarchy (you loop over each item in the MasterTableView and check whether or not each item is expanded or not), but how could you add columns to a detail grid at any level of the hierarchy?


I am having the same issue as an unanswered question in the second half of this thread (by Bruno):
http://www.telerik.com/community/forums/aspnet-ajax/grid/dynamically-add-columns-to-existing-radgrid.aspx

Thanks,
Donald

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 27 Jan 2012, 01:59 PM
Hello Donald,

Sorry, but you have to add dynamically added control on every postback.

for more info click on this link.

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Donald
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or