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

2 Level Hierarchy without SQL

3 Answers 36 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Josh
Top achievements
Rank 1
Josh asked on 21 Dec 2009, 05:36 PM
Hello,

I am trying to create a grid with 2-level self referencing hierarchy. However, the client does not have direct access to the database; it can only request a dataset for a given key. Is there any way to do this without looping through and loading the entire table into the dataset?

This is my current code (TCDataSet is inherited from the DataSet object):
protected void loadGrid() 
    { 
        // Get the null level type codes
        TCDataSet typeCodeDataset = controller.GetTypeCode(null); 
        DataView typeCodeDataview = typeCodeDataset.typecode.DefaultView; 

         
        // Bind the RadGrid's top level view to the null level type codes 
        RadGrid1.MasterTableView.DataSource = typeCodeDataview; 
        RadGrid1.MasterTableView.DataKeyNames = new string[] { "type_code" }; 
        RadGrid1.MasterTableView.EnableColumnsViewState = false
 
        // Create datasets for sub level typecodes 
        TCDataSet[] subTypeCodes = new TCDataSet[typeCodeDataview.Count]; 
        DataView[] subTypeCodeViews = new DataView[typeCodeDataview.Count]; 
        GridTableView[] subTypeCodeTableViews = new GridTableView[typeCodeDataview.Count]; 
        for (int i = 0; i < typeCodeDataview.Count; i++) 
        { 
            subTypeCodes[i] = new TCDataSet(); 
            subTypeCodeViews[i] = subTypeCodes[i].typecode.DefaultView; 
            subTypeCodeTableViews[i] = new GridTableView(); 
            subTypeCodeTableViews[i].DataSource = subTypeCodeViews[i]; 
            subTypeCodeTableViews[i].DataKeyNames = new string[2] { "type_code""type_value" }; 
            RadGrid1.MasterTableView.DetailTables.Insert(i, subTypeCodeTableViews[i]); 
            GridBoundColumn subColumn = new GridBoundColumn(); 
            RadGrid1.MasterTableView.DetailTables[i].Columns.Add(subColumn); 
            subColumn.DataField = "type_code"
            subColumn.HeaderText = "Type Code"
            subColumn.HeaderStyle.Width = 80; 
        } 
 
        //Column to use to create the RadGrid columns 
        GridBoundColumn boundColumn; 
 
        //type_code column 
        boundColumn = new GridBoundColumn(); 
        RadGrid1.MasterTableView.Columns.Add(boundColumn); 
        boundColumn.DataField = "type_code"
        boundColumn.HeaderText = "Type Code"
        boundColumn.HeaderStyle.Width = 80;//Unit.Pixel(20); 
 
        //type_value column 
        boundColumn = new GridBoundColumn(); 
        RadGrid1.MasterTableView.Columns.Add(boundColumn); 
        boundColumn.DataField = "type_value"
        boundColumn.HeaderText = "Type Value"
        boundColumn.HeaderStyle.Width = 80;// Unit.Pixel(30); 
 
        int gridWidth = 0; 
        for (int i = 0; i < RadGrid1.MasterTableView.Columns.Count; i++) 
        { 
            gridWidth += (int)RadGrid1.MasterTableView.Columns[i].HeaderStyle.Width.Value; 
        } 
        RadGrid1.Width = gridWidth + 30; 
    } 
 
 
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        int index = e.Item.ItemIndex; 
        subTypeCodes[index] = controller.GetTypeCode(typeCodeDataview[index]["type_value"].ToString()); 
        subTypeCodeViews[index] = subTypeCodes[index].typecode.DefaultView; 
        subTypeCodeTableViews[index].Rebind(); 
 
    } 

Basically what I'm trying to do here is create a dataset for every master row, and then populate it when the row is expanded. When I click the 'expand' button, the grid seems to disappear.

3 Answers, 1 is accepted

Sort by
0
Josh
Top achievements
Rank 1
answered on 22 Dec 2009, 05:50 PM
Please disregard this question. I have come up with another solution: bind an empty dataset to the detail table and add data to it when a master row is expanded.
0
Accepted
Pavlina
Telerik team
answered on 23 Dec 2009, 05:06 PM
Hello Josh,

You could define the DetailTable directly under the MasterTableView tag and wire the DetailTableDataBind event handling as shown in the articles bellow:
http://www.telerik.com/DEMOS/ASPNET/Prometheus/Grid/Examples/Programming/DetailTableDataBind/DefaultCS.aspx
http://www.telerik.com/help/aspnet-ajax/grdhierarchicaldatabindingusingdetailtabledatabind.html

I hope this information helps.

Kind regards,
Pavlina
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.
0
Josh
Top achievements
Rank 1
answered on 23 Dec 2009, 09:33 PM
Hello Pavlina,

Thank you for your reply. I had already converted to the NeedDataSource and DetailTableDataBind method at the time of your posting. It seemed to work at first, but I still got the error when I tried to change the page on the grid.

I have now changed the grid structure to be defined in the aspx code as shown in the demo instead of created at runtime. The grid now functions as intended.

Thanks again,
Josh
Tags
Grid
Asked by
Josh
Top achievements
Rank 1
Answers by
Josh
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or