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

Programmatically add a column to an existing detail table in hierarchy

5 Answers 573 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Donald
Top achievements
Rank 1
Donald asked on 24 Jan 2012, 07:31 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 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

5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 24 Jan 2012, 08:47 AM
Hello,

protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
    {
 
        //GridBoundColumn b = new GridBoundColumn();
        //b.HeaderText = "test";
        //b.DataField = "Name";
        //e.DetailTableView.Columns.Add(b);
 
    }
    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();
            }
 
        }
    }

You got this issue because the dynamically added control/Item needs re add again on every postback time.
Please remove your code from DetailtableDataBind and added this code in to prerender event.

Thanks,
Jayesh Goyani
0
Donald
Top achievements
Rank 1
answered on 25 Jan 2012, 12:30 AM
Hi Jayesh,

Thank you for your solution. It works fine 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, without nesting for loops?

Thanks,
Donald
0
Tsvetina
Telerik team
answered on 26 Jan 2012, 05:01 PM
Hi Donald,

Please note what one of the user replies there said:
RadGrid does not support mixing declarative grid columns with grid columns added dynamically at runtime. You should either create all the columns in the grid programmatically, or else define them all in the ASPX file.

You can see examples of programmatically created hierarchical grid both in the programmatic creation documentation and demos:
Programmatic Creation (help)
Grid / On PageInit
Grid / On PageLoad

If you need some columns to not be shown at all time, you could change their Visible property in RadGrid PreRender event.

Regards,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Donald
Top achievements
Rank 1
answered on 27 Jan 2012, 02:07 AM
Hi Tsvetina,

In those help articles, columns are only declared in Page_Load or Page_Init. However, in my case, depending on the row that is expanded, the detailtable columns change.

Eg. I have a table called "shapes" with rows "rectangle" and "circle". Expanding the row "square" I get a detailtable with the columns "length" and "width". Expanding the row "circle" I get a detailtable with the column "radius".

If I create all the columns in the grid programmatically, how can I declare the columns in the Grid_DetailTableDataBind event handler?

Thanks,
Donald
0
Accepted
Tsvetina
Telerik team
answered on 31 Jan 2012, 04:02 PM
Hi Donald,

If you want the grid to function correctly, its columns can be created only in Page_Load or Page_Init. Creation in other methods is not supported by the control. Instead of creating or not the columns in question, I advise you to create them all but set Visible or Display = false; for those which should not be Visible. This is in case the set of columns is always the same.
If you are going to have different columns based on datasource, you would need to make a call to the database in Page_Init and check which columns will be needed.

Regards,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Donald
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Donald
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or