RadGrid for ASP.NET

Changing the grid structure dynamically on postback Send comments on this topic.
Defining Structure > Changing the grid structure dynamically on postback

Glossary Item Box

There are cases in which you may want to modify the columns collection of Telerik RadGrid dynamically on postback request - for example choosing an option from control which resides outside of the grid. Thus you can give the user the freedom to alter the grid columns structure when needed.

This is quite straight-forward task having in mind that the entire grid along the columns collection should be modified on PageInit in order to keep the control viewstate consistent and to ensure that its built-in features (like sorting, paging, etc.) will function normally. There is one additional property setting (EnableColumnViewState = false) which has to be configured in order to "tell" the grid that the columns may vary at some stage of the page lifecycle.

The forthcoming code snippets present how to change the columns in a dynamically created grid on dropdown list selection. Note that we fetch the selected value in the dropdown list from the Request.Forms collection passing the id of the combobox:

ASPX/ASCX Copy Code
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
 
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
               
<asp:ListItem Text="empty grid" Value="0" />
               
<asp:ListItem Text="1 column grid" Value="1" />
               
<asp:ListItem Text="2 column grid" Value="2" />
               
<asp:ListItem Text="3 column grid" Value="3" />
 
</asp:DropDownList>
 
<br />
<
asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/Grid/Data/Access/Nwind.mdb"
 
SelectCommand="SELECT ContactName, ContactTitle, Address FROM [Customers]" />
C# Copy Code
protected override void OnInit(EventArgs e)
{
       
base.OnInit(e);
       PopulateGridOnPageInit();
}
protected void PopulateGridOnPageInit()
{
        
string ddlValue = Request.Form.Get("DropDownList1");

       RadGrid RadGrid1 =
new RadGrid();
       RadGrid1.ID =
"RadGrid1";
       RadGrid1.Width = Unit.Pixel(400);
       RadGrid1.AllowSorting = true;
       RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
       RadGrid1.AllowPaging = true;
       RadGrid1.Skin =
"Mac";
       RadGrid1.DataSourceID =
"AccessDataSource1";

       RadGrid1.MasterTableView.AutoGenerateColumns = false;
       RadGrid1.MasterTableView.EnableColumnsViewState = false;

       GridBoundColumn boundColumn;

        
if (ddlValue != null)
       {
           
switch (ddlValue)
           {

                
case "0":
                   RadGrid1.DataSourceID =
"";
                   RadGrid1.DataSource =
new object[0];
                   
break;
                
case "1":
                   boundColumn =
new GridBoundColumn();
                   boundColumn.HeaderText =
"ContactName";
                   boundColumn.DataField =
"ContactName";
                   boundColumn.UniqueName =
"ContactName";
                   RadGrid1.MasterTableView.Columns.Add(boundColumn);

                   
break;

                
case "2":
                   boundColumn =
new GridBoundColumn();
                   boundColumn.HeaderText =
"ContactName";
                   boundColumn.DataField =
"ContactName";
                   boundColumn.UniqueName =
"ContactName";
                   RadGrid1.MasterTableView.Columns.Add(boundColumn);

                   boundColumn =
new GridBoundColumn();
                   boundColumn.HeaderText =
"ContactTitle";
                   boundColumn.DataField =
"ContactTitle";
                   boundColumn.UniqueName =
"ContactTitle";
                   RadGrid1.MasterTableView.Columns.Add(boundColumn);

                   
break;

                
case "3":
                   boundColumn =
new GridBoundColumn();
                   boundColumn.HeaderText =
"ContactName";
                   boundColumn.DataField =
"ContactName";
                   boundColumn.UniqueName =
"ContactName";
                   RadGrid1.MasterTableView.Columns.Add(boundColumn);

                   boundColumn =
new GridBoundColumn();
                   boundColumn.HeaderText =
"ContactTitle";
                   boundColumn.DataField =
"ContactTitle";
                   boundColumn.UniqueName =
"ContactTitle";
                   RadGrid1.MasterTableView.Columns.Add(boundColumn);

                   boundColumn =
new GridBoundColumn();
                   boundColumn.HeaderText =
"Address";
                   boundColumn.DataField =
"Address";
                   boundColumn.UniqueName =
"Address";
                   RadGrid1.MasterTableView.Columns.Add(boundColumn);

                   
break;
           }
       }
        
else
       {
           
RadGrid1.DataSourceID = "";
           RadGrid1.DataSource =
new object[0];
       }
           PlaceHolder1.Controls.Add(RadGrid1);

}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
       RadGrid grid = PlaceHolder1.FindControl(
"RadGrid1") as RadGrid;
       grid.Rebind();
}
VB.NET Copy Code
Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
 MyBase.OnInit(e)
 PopulateGridOnPageInit
End Sub

Protected Sub PopulateGridOnPageInit()
 Dim ddlValue As String = Request.Form.Get("DropDownList1")
 Dim RadGrid1 As RadGrid = New RadGrid

 RadGrid1.ID = "RadGrid1"
 RadGrid1.Width = Unit.Pixel(400)
 RadGrid1.AllowSorting = True
 RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric
 RadGrid1.AllowPaging = True
 RadGrid1.Skin = "Mac"
 RadGrid1.DataSourceID = "AccessDataSource1"

 RadGrid1.MasterTableView.AutoGenerateColumns = False
 RadGrid1.MasterTableView.EnableColumnsViewState = False

 Dim boundColumn As GridBoundColumn

 If Not (ddlValue Is Nothing) Then

   Select Case ddlValue

   Case "0"
     RadGrid1.DataSourceID = ""
     RadGrid1.DataSource = New Object() {}

   Case "1"
     boundColumn = New GridBoundColumn
     boundColumn.HeaderText = "ContactName"
     boundColumn.DataField = "ContactName"
     boundColumn.UniqueName = "ContactName"
     RadGrid1.MasterTableView.Columns.Add(boundColumn)

   Case "2"
     boundColumn = New GridBoundColumn
     boundColumn.HeaderText = "ContactName"
     boundColumn.DataField = "ContactName"
     boundColumn.UniqueName = "ContactName"
     RadGrid1.MasterTableView.Columns.Add(boundColumn)

     boundColumn = New GridBoundColumn
     boundColumn.HeaderText = "ContactTitle"
     boundColumn.DataField = "ContactTitle"
     boundColumn.UniqueName = "ContactTitle"
     RadGrid1.MasterTableView.Columns.Add(boundColumn)

   Case "3"
     boundColumn = New GridBoundColumn
     boundColumn.HeaderText = "ContactName"
     boundColumn.DataField = "ContactName"
     boundColumn.UniqueName = "ContactName"
     RadGrid1.MasterTableView.Columns.Add(boundColumn)

     boundColumn = New GridBoundColumn
     boundColumn.HeaderText = "ContactTitle"
     boundColumn.DataField = "ContactTitle"
     boundColumn.UniqueName = "ContactTitle"
     RadGrid1.MasterTableView.Columns.Add(boundColumn)

     boundColumn = New GridBoundColumn
     boundColumn.HeaderText = "Address"
     boundColumn.DataField = "Address"
     boundColumn.UniqueName = "Address"
     RadGrid1.MasterTableView.Columns.Add(boundColumn)

   End Select
 Else
   RadGrid1.DataSourceID = ""
   RadGrid1.DataSource = New Object() {}
 End If

 PlaceHolder1.Controls.Add(RadGrid1)
End Sub

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
 Dim grid As RadGrid = CType(PlaceHolder1.FindControl("RadGrid1"), RadGrid)
 grid.Rebind()
End Sub