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

[Solved] Hierarchy with 2 Tables for details

1 Answer 246 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Officialboss
Top achievements
Rank 1
Officialboss asked on 24 Jun 2008, 05:31 PM
Hello,

I was wondering if anyone could suggest on how to do a Hierarchy RadGrid where the Source Table for details depends on a field in the Parent table.

For example;
My parent table has a Boolean field with 1 and 0.
For the Details or second level Hierarchy, if Parent row is 1 then I get data from Table A1 if 0 then I get data from Table A2.

Hope I was able to make my scenario clear.

Thanks

1 Answer, 1 is accepted

Sort by
0
Baatezu
Top achievements
Rank 2
answered on 24 Jun 2008, 11:14 PM
First - I'd recommend referencing http://www.telerik.com/help/aspnet-ajax/grdhierarchicaldatabindingusingdetailtabledatabind.html for some pointers. I expect that will mostly cover your specific case. I'll go into more detail for anyone else that sees this thread and needs a little more info as I did.
The code I am basing my examples from has been modified from what it actually was. Mostly to simplify it.

My RadGrid ASCX details
<telerik:RadGrid ID="grdCompanyTypes" runat="server" AutoGenerateColumns="False"  
    GridLines="None" Skin="WebBlue"
    <MasterTableView DataKeyNames="CompanyTypeID"
        <DetailTables> 
            <telerik:GridTableView runat="server" DataKeyNames="ID" CommandItemDisplay="Top" Name="Companies" AllowSorting="True" PageSize="50"
                <ParentTableRelation> 
                    <telerik:GridRelationFields DetailKeyField="CompanyTypeID" MasterKeyField="CompanyTypeID" /> 
                </ParentTableRelation> 
                <RowIndicatorColumn Visible="False"
                    <HeaderStyle Width="20px" /> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn Resizable="False"
                    <HeaderStyle Width="20px" /> 
                </ExpandCollapseColumn> 
                <PagerStyle Mode="NextPrevAndNumeric" /> 
            </telerik:GridTableView> 
        </DetailTables> 
        <RowIndicatorColumn Visible="False"
        <HeaderStyle Width="20px"></HeaderStyle> 
        </RowIndicatorColumn> 
        <ExpandCollapseColumn Resizable="False"
        <HeaderStyle Width="20px"></HeaderStyle> 
        </ExpandCollapseColumn> 
        <PagerStyle Mode="NextPrevAndNumeric" /> 
    </MasterTableView> 
</telerik:RadGrid> 

There are a couple things that are from the referenced article in the above code that may not be needed in the way I do my C# code. Specifically the ParentTableRelation section. I haven't tried taking it out so I don't know. It was used in articles and demo's I used to build my application, and they have remained, needed or not. The DataKeyNames are used in accessing the DetailTable information

I'll address the Code Behind functions now. I don't have any linked in the ASCX, but I'm sure you can create the connections. :)
First - NeedDataSource
protected void grdCompanyTypes_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    if (!e.IsFromDetailTable) 
    { 
        grdCompanyTypes.DataSource = YOURDATASOURCE; 
    } 
This is pretty normal for my grids, except the IsFromDetailTable - This if prevents the DetailTables from refreshing the MasterTable. The YOURDATASOURCE is however you get your data. I have a few custom functions to get it, so I removed them to prevent the confusion. I'll do this in the rest of my code examples as well.
To DataBind the child tables, you will use DetailTableDataBind, mine looks like
protected void grdCompanyTypes_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e) 
    switch (e.DetailTableView.Name) 
    { 
        case "Companies"
            string CompanyTypeID = e.DetailTableView.ParentItem.GetDataKeyValue("CompanyTypeID").ToString(); 
            e.DetailTableView.DataSource = YOURDATASOURCE; 
            break
        case "Reps"
            string ID = e.DetailTableView.ParentItem.GetDataKeyValue("ID").ToString(); 
            e.DetailTableView.DataSource = YOURDATASOURCE; 
            break
    } 
This set up allows for multiple DetailTables. If you have just 1, you could ignore the switch statement. As you can see from the example, My 3rd (removed in the ASCX) table is called Reps. The GetDataKeyValye("CompanyTypeID") is used to get the value I used to decide what DetailTable records to pull. If your filed names was MyBooleanValue you would use GetDataKeyValue("MyBooleanValue"). I'll just note - This is one of the fields that was pulled from the DataBind function, otherwise it will cause an error. In your specific case, you could do an IF statement and then pull the data however from where ever based on the information. I just needed to pull some linked data, so I don't have any logic based on my CompanyTypeID.

Next - Since we have data in a grid, I'm sure we want to manipulate the data, Insert/Edit/Delete. These functions are done very similar to the DetailTableDataBind function. The difference is in how the key bits of information is pulled. My three functions look like
protected void grdCompanyTypes_UpdateCommand(object source, GridCommandEventArgs e) 
    switch (e.Item.OwnerTableView.Name) 
    { 
        case "Companies"
            Label lbl = (Label)e.Item.FindControl("lblCompanyID_Edit"); 
            break
        case "Reps"
            //Same style as above 
            break
        default
            break
    } 
 
protected void grdCompanyTypes_DeleteCommand(object source, GridCommandEventArgs e) 
    switch (e.Item.OwnerTableView.Name) 
    { 
        case "Companies"
            e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"]; 
            break
        case "Reps"
            //Same as above 
            break
        default
            break
    } 
 
protected void grdCompanyTypes_InsertCommand(object source, GridCommandEventArgs e) 
    switch (e.Item.OwnerTableView.Name) 
    { 
        case "Companies"
            e.Item.OwnerTableView.ParentItem.GetDataKeyValue("CompanyTypeID"); 
            RadTextBox rtb = (RadTextBox)e.Item.FindControl("txtCompanyName"); 
            break
        case "Reps"
            //Same as above 
            break
        default
            break
    } 
The FindControl("MyControlName") is used to get the information that is being added/updated. Like I normally do when using a grid control.
In 2 of these I also use the GetDataKeyValue function to process the request. I don't know if you will need it or not. It took me a couple hours to hunt down how to get the value I needed to do my things, so I've left it in so it might help if  anyone else needs it.

I do believe this covers everything I've done to get my multilevel Grid control working. It just misses a few specific details which will be different for each application, and the ASCX is limited to 1 Detail table, while my code shows 2.

Hopefully this all makes sense and has been helpful.
Tags
Grid
Asked by
Officialboss
Top achievements
Rank 1
Answers by
Baatezu
Top achievements
Rank 2
Share this question
or