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

[Solved] Adding Detail Table in OnItemCommand event

1 Answer 129 Views
Grid
This is a migrated thread and some comments may be shown as answers.
pavan pemmaraju
Top achievements
Rank 1
pavan pemmaraju asked on 05 Oct 2009, 04:16 PM
Guys,
I want to be able to add and display a detail table based on the griditem in the master table. I have an image button column upon clicking which, in the onItemCommand event handler I check the griditem and am trying to add a detail table to the grid if necessary and assign a datasource but it is not working. Anyone have any ideas on how to make this work. Please note that on the page init I have no way knowing about the detail table view. Below is the code.

Thanks,
Pavan.

 public partial class _Default : System.Web.UI.Page
    {

        IList<DataItem> DataSource;

        private void BuildSource()
        {
            List<DataItem> DataItemList = new List<DataItem>();
            for (int i = 0; i < 5; i++)
            {
                DataItem di = new DataItem();
                di.CustomerID = i.ToString();
                di.ContactName = "Customer_" + i;
                DataItemList.Add(di);
            }
            DataSource = DataItemList;
        }


        private void DefineGridStructure()
        {
            RadGrid RadGrid1 = new RadGrid();

            RadGrid1.ID = "RadGrid1";
            RadGrid1.DataSource = DataSource;

            RadGrid1.MasterTableView.DataKeyNames = new string[] { "CustomerID" };

            RadGrid1.Width = Unit.Percentage(98);
            RadGrid1.PageSize = 3;
            RadGrid1.AllowPaging = true;
            RadGrid1.AllowSorting = true;
            RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
            RadGrid1.AutoGenerateColumns = false;
            RadGrid1.ShowStatusBar = true;
            RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);

            RadGrid1.MasterTableView.PageSize = 20;


            //Add columns

            GridButtonColumn buttonColumn;
            buttonColumn = new GridButtonColumn();
            buttonColumn.ButtonType = GridButtonColumnType.ImageButton;
            buttonColumn.CommandName = "expand";
            buttonColumn.HeaderText = "";
            buttonColumn.ImageUrl = "expand.gif";
            buttonColumn.UniqueName = "expand";
            buttonColumn.Text = "expand";
            RadGrid1.MasterTableView.Columns.Add(buttonColumn);
          
            GridBoundColumn boundColumn;
            boundColumn = new GridBoundColumn();
            boundColumn.DataField = "CustomerID";
            boundColumn.HeaderText = "CustomerID";
            RadGrid1.MasterTableView.Columns.Add(boundColumn);

            boundColumn = new GridBoundColumn();
            boundColumn.DataField = "ContactName";
            boundColumn.HeaderText = "Contact Name";
            RadGrid1.MasterTableView.Columns.Add(boundColumn);
            form1.Controls.Add(RadGrid1);

            
        }

        protected void Page_Init(object source, System.EventArgs e)
        {
            BuildSource();
            DefineGridStructure();
        }


        protected void Page_Load(object sender, EventArgs e)
        {

        }

       private void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
       {

           if (e.CommandName == "expand")
           {
               RadGrid grid = (RadGrid)form1.FindControl("RadGrid1");

               GridTableView tableview1 = new GridTableView(grid);
               tableview1.DataSource = DataSource;

               GridBoundColumn boundColumn;
               boundColumn = new GridBoundColumn();
               boundColumn.DataField = "CustomerID";
               boundColumn.HeaderText = "CustomerID";
               tableview1.Columns.Add(boundColumn);

               boundColumn = new GridBoundColumn();
               boundColumn.DataField = "ContactName";
               boundColumn.HeaderText = "Contact Name";
               tableview1.Columns.Add(boundColumn);

               grid.MasterTableView.DetailTables.Add(tableview1);
           }
            
        }
    }

1 Answer, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 08 Oct 2009, 10:44 AM
Hello pavan,

Adding detail tables to RadGrid dynamically on postbacks events is not supported by the control because this will corrupt its viewstate and lifecycle. The only way to build hierarchy programmatically is inside the OnInit handler as explained in the relevant section of this documentation topic:

http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html

For your scenario the option I would suggest are either:

- Build the entire grid on Init and hide the detail tables that do not need to be displayed initially. At a later stage of the lifecycle change the visibility of these child tables programmatically

or

- Create different grid instances inside the OnInit handler (based on your custom conditions) and remove the previously generated grids from the Controls collection of their container.

Best regards, Sebastian
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
pavan pemmaraju
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Share this question
or