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

Dynamically Created Hierarchical Grid

1 Answer 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Adam Nelson
Top achievements
Rank 2
Adam Nelson asked on 01 Oct 2008, 06:13 PM
I have a scenario in which a user may select from a number of predefined queries which loads a RadGrid. The grid is generated dynamically as the columns may vary from query to query. This is all working until I tried adding DetailTables for the MasterTableView. I tried to recreate my issue by following examples in documentation and it all worked, until I added a button that would simulate the selection of a query. If the grid is added to the control collection initially without any DetailTables it seems like it is impossible to add one later. Is this by design?

Here is the code for my sample application.

protected void Page_Init(object sender, System.EventArgs e)  
{  
    PopulateGridOnPageInit();  
}  
 
private bool LoadGrid()  
{  
    //Hidden field is set true when "Load Grid" button is clicked  
    string loadGrid = Request.Form.Get(this.uxHiddenField_LoadGrid.UniqueID);  
    if (!string.IsNullOrEmpty(loadGrid))  
    {  
        return Boolean.Parse(loadGrid);  
    }  
    else 
    {  
        return false;  
    }  
}  
 
private void PopulateGridOnPageInit()  
{  
    RadGrid radGrid = new RadGrid();  
    radGrid.ID = RadGridId;  
 
    if (!LoadGrid())  
    {  
        radGrid.Visible = false;  
        // Add the Grid to the page  
        uxPlaceHolder.Controls.Add(radGrid);  
        return;  
    }  
    // Set properties  
    radGrid.Skin = "Vista";  
    radGrid.PageSize = 25;  
    radGrid.AllowPaging = true;  
    radGrid.AllowCustomPaging = true;  
    radGrid.AllowSorting = true;  
    radGrid.MasterTableView.AllowCustomSorting = true;  
    radGrid.MasterTableView.EnableColumnsViewState = false;  
    radGrid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;  
 
    radGrid.ClientSettings.Selecting.AllowRowSelect = true;  
    radGrid.ClientSettings.Selecting.EnableDragToSelectRows = false;  
    radGrid.ClientSettings.Resizing.AllowColumnResize = false;  
    radGrid.ClientSettings.Resizing.ClipCellContentOnResize = false;  
    radGrid.ClientSettings.Resizing.EnableRealTimeResize = false;  
    radGrid.ClientSettings.ReorderColumnsOnClient = false;  
    radGrid.ClientSettings.AllowColumnsReorder = false;  
    radGrid.ClientSettings.ColumnsReorderMethod = GridClientSettings.GridColumnsReorderMethod.Reorder;  
 
    radGrid.ShowGroupPanel = false;  
    radGrid.GroupingEnabled = false;  
    radGrid.GroupPanel.Visible = false;  
    radGrid.ShowFooter = false;  
    radGrid.MasterTableView.ShowFooter = false;  
 
    // Selecting multiple rows (client-side)  
    radGrid.AllowMultiRowSelection = true;  
    GridClientSelectColumn gridClientSelectColumn = new GridClientSelectColumn();  
    gridClientSelectColumn.ItemStyle.Width = Unit.Percentage(1);  
    radGrid.Columns.Add(gridClientSelectColumn);  
 
    // Build the columns dynamically  
    radGrid.AutoGenerateColumns = false;  
 
    GridBoundColumn column = new GridBoundColumn();  
    column.DataField = "ID";  
    column.HeaderText = "Customer Identifier";  
    radGrid.Columns.Add(column);  
    column = new GridBoundColumn();  
    column.DataField = "Name";  
    column.HeaderText = "Name";  
    radGrid.Columns.Add(column);  
    column = new GridBoundColumn();  
    column.DataField = "Company";  
    column.HeaderText = "Company";  
    radGrid.Columns.Add(column);  
    GridTableView childTable = new GridTableView(radGrid);  
    childTable.Width = Unit.Percentage(100);  
    childTable.DataMember = "CustomerID";  
    column = new GridBoundColumn();  
    column.DataField = "ID";  
    column.HeaderText = "Order Identifier";  
    childTable.Columns.Add(column);  
    column = new GridBoundColumn();  
    column.DataField = "OrderDate";  
    column.HeaderText = "Order Date";  
    childTable.Columns.Add(column);  
    column = new GridBoundColumn();  
    column.DataField = "Cost";  
    column.HeaderText = "Cost";  
    childTable.Columns.Add(column);  
    radGrid.MasterTableView.DetailTables.Add(childTable);              
 
    // Wire up the method that will get the grid data  
    radGrid.NeedDataSource += new GridNeedDataSourceEventHandler(radGrid_NeedDataSource);  
 
    radGrid.DetailTableDataBind += new GridDetailTableDataBindEventHandler(radGrid_DetailTableDataBind);  
 
    // Set the key for client side selection  
    radGrid.MasterTableView.ClientDataKeyNames = new string[] { "ID" };  
 
    // Set the key for server side selection  
    radGrid.MasterTableView.DataKeyNames = new string[] { "ID" };  
 
    // Add the Grid to the page  
    uxPlaceHolder.Controls.Add(radGrid);  
}  
 
protected void radGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
{  
    if (!e.IsFromDetailTable)  
    {  
        // Get the rad grid  
        RadGrid radGrid = (RadGrid)source;  
 
        // Bind it to the Grid  
        radGrid.DataSource = this.Customers;  
 
        // Set the count  
        radGrid.VirtualItemCount = this.Customers.Count;  
    }  
}  
 
protected void radGrid_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)  
{  
    GridDataItem dataItem = e.DetailTableView.ParentItem;  
    switch (e.DetailTableView.DataMember)  
    {  
        case "CustomerID":  
            List<Order> orders = GetOrdersForCustomer((int)dataItem.GetDataKeyValue("ID"));  
            e.DetailTableView.DataSource = orders;  
            e.DetailTableView.VirtualItemCount = orders.Count;  
            break;  
        default:  
            break;  
    }  


Any thoughts or advice would be very appreciated.

Thanks,

Adam

1 Answer, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 03 Oct 2008, 09:10 AM
Hi Adam,

Please try modifying your code as shown below and check if it makes any difference:

if (!LoadGrid())  
{  
    radGrid.Style["display"] = "none";  
    // Add the Grid to the page  
    //uxPlaceHolder.Controls.Add(radGrid);  
    //return;  

Find more about RadGrid Visible/ conventions here.

Let me know how it goes.

Kind regards,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Adam Nelson
Top achievements
Rank 2
Answers by
Iana Tsolova
Telerik team
Share this question
or