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

Hierarchical Grid and Sorting Child Tables

1 Answer 52 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 09 Sep 2013, 04:09 PM
Hello,

I have created a hierarchical RADGrid which seems for the most part to be working fine. The issue that I am having is with sorting the rows in the child table. First, let me show how data is being bound:
protected void rgSalesAnalysis_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{             
            bool ByCode = rbSoldSummary.Checked;
 
            if (ByCode)
            {
                rgSalesAnalysis.MasterTableView.GetColumn("First").HeaderText = "Barcode";
                rgSalesAnalysis.MasterTableView.DetailTables[0].GetColumn("Second").HeaderText = "Location";
            }
            else
            {
                rgSalesAnalysis.MasterTableView.GetColumn("First").HeaderText = "Location";
                rgSalesAnalysis.MasterTableView.DetailTables[0].GetColumn("Second").HeaderText = "Barcode";
            }
            List<SaleAnalysisRow> analysisData = sa.GetAnalysisData(ByCode, ....)ToList();
            rgSalesAnalysis.DataSource = analysisData;
}
 
//----------------------------------------------------------------------------------------------------------------------------
 
protected void rgSalesAnalysis_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
{
            e.DetailTableView.DataSource = ((SaleAnalysisRow)e.DetailTableView.ParentItem.DataItem).Details;
}

Now when I try to sort the child table, I receive the error: Object reference not set to an instance of an object. 
     e.DetailTableView.DataSource = ((SaleAnalysisRow)e.DetailTableView.ParentItem.DataItem).Details; Line 102:       
}

Obviously the "ParentItem" is null, but I am unsure how to proceed at this point to fill that object and sort the data in the child grid.

Thank you.







1 Answer, 1 is accepted

Sort by
0
Matt
Top achievements
Rank 1
answered on 10 Sep 2013, 08:39 PM
I have figured this out for anyone interested. Basically when the detail table is re bound, it simply needs the data source set. I had to add a unique id as a datakey and based on that, populate the detail table with the specific information from my sales object:

protected void rgSalesAnalysis_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
        {
            if (isFromDetailTable)
            {
                string Id = e.DetailTableView.ParentItem.GetDataKeyValue("UniqueId").ToString();
                List<SaleAnalysisRow> row = (List<SaleAnalysisRow>)e.DetailTableView.DataSource;
                SaleAnalysisRow r = row.Where(z => z.UniqueId == Convert.ToInt32(Id)).FirstOrDefault();
                e.DetailTableView.DataSource = r.Details;
            }
            else
                e.DetailTableView.DataSource = ((SaleAnalysisRow)e.DetailTableView.ParentItem.DataItem).Details;
        }
Tags
Grid
Asked by
Matt
Top achievements
Rank 1
Answers by
Matt
Top achievements
Rank 1
Share this question
or