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

DetailTables with different # of columns at same level in a RadGrid Hierarchy?

9 Answers 397 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Adam
Top achievements
Rank 1
Adam asked on 09 Sep 2011, 04:13 PM
Looking at the examples at http://www.telerik.com/help/aspnet-ajax/grid-understanding-hierarchy.html and at http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html seems to imply that the DetailTables at the same level in a RadGrid hierarchy must all share that same layout. For example, for a 3-level hierarchy:

MasterTableView
+Item(0)
DetailTable0(copy0)
+Item(0)
DetailTable00(copy0)
+Item(0)
+Item(1)
+Item(2)
...
+Item(1)
DetailTable00(copy1)
+Item(0)
+Item(1)
+Item(2)
...

...
...

The DetailTables look like they need to be copies of each other, implying their items share the same layout (column names/number, etc).

Is there an easy way to allow the 3rd level of the hierarchy (the DetailTable00 items) to have different # of columns? The structure I want is like this:

Type1 - (Name, Comments)
Category1 - (Name, Comments)
Item1 - (Name, Yes, No, Comments)
Item2 - (Name, Yes, No, Comments)
...
Category2 - (Name, Comments)
Item1 - (Name, Low, Mid, High, Comments)
Item2 - (Name, Low, Mid, High, Comments)
...
...
Type2 - (Name, Comments)
Category1 - (Name, Comments)
Item1 - (Name, Date, Comments)
Item2 - (Name, Date, Comments)
...
...
...

Type and Category columns will be the same, but the items within each CategoryX could have a slightly different layout compared to another category (because there will be columns for a scale rating, and the scale type can vary depending on the category!). Each category's items would be homogeneous, of course; the variation would be between categories.

Can I accomplish something like this without too much complication?

9 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 14 Sep 2011, 12:34 PM
Hi Adam,

To achieve your goal you can use a NestedViewTemplate with a grid in it and then change the datasource of the grid based on the parent item condition. Please refer to the link below for a live example:
http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/nestedviewtemplate/defaultcs.aspx

All the best,
Pavlina
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal
0
Adam
Top achievements
Rank 1
answered on 15 Sep 2011, 05:39 PM
Thank you Pavlina, that's exactly what I want to do! I've almost got it working, but I'm having some issues populating the RagGrid inside the template.

I have the following setup (edited down for structural clarity):

<telerik:RadGrid ID="rgCapabiltyEntries" runat="server" DataSourceID="SqlDataSource1"
     <!-- TYPES -->
     <MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="typeid"
          <DetailTables>
               <!-- CATEGORIES -->
               <telerik:GridTableView runat="server" DataSourceID="SqlDataSource2" DataKeyNames="categoryid">
         <ParentTableRelation>
             <telerik:GridRelationFields DetailKeyField="type" MasterKeyField="typeid" />
          </ParentTableRelation>
 
          <NestedViewSettings DataSourceID="SqlDataSource3">
             <ParentTableRelation>
                <telerik:GridRelationFields DetailKeyField="category" MasterKeyField="categoryid" />
             </ParentTableRelation>
          </NestedViewSettings>
          <NestedViewTemplate>
                          <!-- CATEGORY ITEMS -->
              <telerik:RadGrid ID="rgCapabiltyItems" runat="server" DataSourceID="SqlDataSource3">
                 <MasterTableView DataSourceID="SqlDataSource3">                                       
                 </MasterTableView>
              </telerik:RadGrid>
          </NestedViewTemplate>
 
      </telerik:GridTableView>
   </DetailTables>                   
     </MasterTableView>              
</telerik:RadGrid>

Here are the SqlDataSource code blocks:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
        SelectCommand="SELECT * FROM [CapabilityTypes]">
</asp:SqlDataSource>
 
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
        ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
        SelectCommand="
SELECT  
                cc.*,
                cs.scalename,
                cst.scaletype
FROM   
                [Test].[dbo].[CapabilityCategories] AS cc  INNER JOIN
                [Test].[dbo].[CapabilityScales] AS cs ON cc.scale = cs.id INNER JOIN
                [Test].[dbo].[CapabilityScaleTypes] AS cst ON cs.scaletype = cst.id
WHERE  
                ([type] = @type)
">
<SelectParameters>
     <asp:Parameter Name="type" Type="Int32" />
</SelectParameters>
 
</asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server"
        ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
        SelectCommand="
SELECT 
                ci.category,
                ci.itemname,
                ce.rating,
                ce.date,
                ce.comments,
                ce.lastUpdated,
                e.name
FROM   
                [Test].[dbo].[CapabilityItems] AS ci  LEFT OUTER JOIN
                [Test].[dbo].[CapabilityEntries] AS ce ON ce.item = ci.id  LEFT OUTER JOIN
                [Test].[dbo].[Employees] AS e ON e.id = ce.employee
WHERE  
                ci.category = @id AND ce.site = @site
ORDER BY
                ci.id
">
<SelectParameters>
     <asp:Parameter Name="id" Type="Int32" />  <---- THIS IS THE PARAMETER THAT DOESN'T SEEM TO LOAD CORRECTLTY; REPLACE WITH HARDCODED VALUE AND TABLE PULLS FOR THAT CATEGORY ID
     <asp:SessionParameter Name="site" SessionField="siteNumber" />
</SelectParameters>
</asp:SqlDataSource>


Looking at the example you linked, it looks like I want to work on the category items RadGrid inside the ItemCreated event, whenever one of the CATEGORIES Grid rows is created. Then I can build the data source and bind it dynamically, and the event information will have a reference to the row data I need (specifically the category id).

The example linked uses this logic -   if (e.Item is GridNestedViewItem)  - is there a way to differentiate between the DetailTables nested item (with the CATEGORIES Grid) and the NestedViewTemplate nested item (with the RadGrid)? I want to be able to reference a value in the CATEGORIES Grid so that I can dynamically add checkbox columns to the template RadGrid based on that value, but the first GridNestedViewItem I see is the catergory DetailTable for the first type.


Also (on a slightly unrelated note), you'll notice that in the code above I'm using a static data source for the template's RadGrid, just to test how it would look. However, I wasn't able to get the parent table relationship to work. It appears that the SQL parameter for the RadGrid (for the 'WHERE item = @parentTableItem' statement) wasn't tying back to the CATEGOTIES GridTableView DataKeyName... any idea what I could be doing wrong? I followed the examples by adding a ParentTableRelation inside a NestedViewSettings, but all the examples have templates inside the MasterView, not an extra level deeper in the hierarchy (inside DetailTables)...


Thanks again,
-Adam
0
Adam
Top achievements
Rank 1
answered on 15 Sep 2011, 11:21 PM
Just a quick update, I'm using this code to find the RadGrid inside the template and populate it:

protected void rgCapabiltyEntries_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridNestedViewItem)
    {
        GridNestedViewItem nestedview = (GridNestedViewItem)e.Item;
 
        if(nestedview.UniqueID.Contains("Detail"))
        {
            RadGrid rgItems = (RadGrid)nestedview.FindControl("rgCapabiltyItems");
 
            int site = int.Parse(Session["siteNumber"].ToString());
            int category = int.Parse(nestedview.ParentItem["categoryNum"].Text);
  
            rgItems.DataSource = GetCapabilityItemsByCategoryAndSite(category, site);
        }
 
        // perform operations
    
}


Is that the best way to distinguish between the different GridNestedViews of the overall heirarchy (looking for the word 'Detail' inside the UniqueID property)? Seems a bit hokey, but I couldn't find any other way to do it :)

I still can't figure out why the  hierarchical relation in my grid with templates won't work, but it's not as important because I'm doing the data binding programmatically in the event handler. Still, I'm getting much closer now, thanks for pointing me in the right direction!


Thanks,
-Adam
0
Pavlina
Telerik team
answered on 21 Sep 2011, 09:15 AM
Hi Adam,

You can access the grid by iterating through all the NestedViewItems and getting reference to the specific control. For more information please check the following help articles:
Traversing detail tables/items in Telerik RadGrid
NestedView template
as well as this forum post on similar issue.

All the best,
Pavlina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Adam
Top achievements
Rank 1
answered on 21 Sep 2011, 02:42 PM
Thank you Pavlina, 

I ended up doing something very similar except I created the nested RadGrid columns dynamically in the "ItemCreated" event (a different RadGrid lives inside each row of the nested gridview), like so:

protected void rgCapabiltyCategories_ItemCreated(object sender, GridItemEventArgs e)
{
     if (e.Item is GridNestedViewItem)
     {
          GridNestedViewItem nestedview = (GridNestedViewItem)e.Item;
          if (nestedview.UniqueID.Contains("Detail"))
          {
               RadGrid rgItems = (RadGrid)nestedview.FindControl("rgCapabiltyItemsByCategory");
               rgItems.ItemCreated += new GridItemEventHandler(rgItems_ItemCreated);
               ...
 
etc, etc


Everything is working fine now, except when I minimize one of the nested items, the entire table disappears. I suspect because the minimizing/expanding triggers a postback, but the grid items are not being re-created, so my table-building code is not being called again. I'm not 100% sure which event I need to tie to for that situation.

The 'Traversing detail tables/items in Telerik RadGrid' link you posted appears to be dead. The forums and help documentation on the Telerik website are top-notch though! They have been very helpful in my not-so-gentle introduction to the control library!

Thanks for the help, I'm going to try and work this out, but it's good to know I can come here for help when I get stuck.


-Adam


0
Adam
Top achievements
Rank 1
answered on 21 Sep 2011, 10:35 PM
So I've completely switched gears, and am now embedding my nested RagGrid inside a 2-level RadPanelBar. It looks better, it's more user-friendly, and I don't have any of the problems with the data clearing when I expand/collapse. This control is awesome, and seems infinitely easier to use than the programmatically created RadGrid hierarchy. I think I was really trying to bend the Grid control to do something it's not optimal for... It was a good way to learn how to manipulate and customize the RadControls, though!

I only have one issue now - the nested RadGrids are being cut off by the RadPanelBar sides. The RadPanelBar seems to only auto-size to the largest root member width... is there any way to force the RadPanelBar width to stretch to fill the containing window/panel (the same way the RadGrid seemed to do automatically)?

I'd also like to force the RadGrid inside the 2nd level RadPanelBar item to fit within the panel. Any idea how to do this?


Is this the right place to ask these questions, or should I report in the PanelBar forums?


Thanks,
-Adam
0
Pavlina
Telerik team
answered on 23 Sep 2011, 02:53 PM
Hi Adam,

I hope the help article below will be useful for you:
http://www.telerik.com/help/aspnet-ajax/panelbar-resize-to-the-width-of-the-widest-radpanelitem.html

All the best,
Pavlina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Adam
Top achievements
Rank 1
answered on 23 Sep 2011, 07:47 PM
I tried that example, and I get this error:


The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).


I am dynamically creating the panel's root and child items in the code behind, so I wonder if that has something to do with the error?

protected void CategoriesPanelBar_Init(object sender, EventArgs e)
{
    DataTable cTypes = GetCapabilityTypes(false);
    DataTable cCategories = GetCapabilityCategories();
 
    RadPanelBar rpbCapabilityCategories = (RadPanelBar)sender;
 
    foreach (DataRow typeRow in cTypes.Rows)
    {
        string typeName = typeRow["typename"].ToString();
        string typeID = typeRow["id"].ToString();
        RadPanelItem typeItem = new RadPanelItem(typeName);
        DataRow[] categoriesByType = cCategories.Select(string.Format("type = {0}", typeID));
        foreach (DataRow categoryRow in categoriesByType)
        {
            string categoryName = categoryRow["categoryname"].ToString();
            int categoryID = int.Parse(categoryRow["id"].ToString());
            RadPanelItem categoryItem = new RadPanelItem(categoryName);
            categoryItem.BackColor = System.Drawing.Color.LightBlue;
 
            RadGrid rgItems = new RadGrid();
            rgItems.AutoGenerateColumns = false;
            rgItems.AllowMultiRowEdit = true;
            rgItems.MasterTableView.EditMode = GridEditMode.InPlace;
            rgItems.PreRender += new EventHandler(rgItems_PreRender);
 
            BuildCapabilityItemsGridByCategory(rgItems, categoryID);
 
            categoryItem.Controls.Add(rgItems);
            typeItem.Items.Add(categoryItem);
        }
 
        rpbCapabilityCategories.Items.Add(typeItem);
    }
 
}


This is also where I'm adding the dynamically created RadGrids to the panel child items.
0
Pavlina
Telerik team
answered on 26 Sep 2011, 08:50 PM
Hi Adam,

To prevent this server error you should use RadCodeBlock. Please refer to this help article for more information on the matter.

Greetings,
Pavlina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Adam
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Adam
Top achievements
Rank 1
Share this question
or