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

Grid in Grid problem...

4 Answers 136 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Igor
Top achievements
Rank 1
Igor asked on 06 Mar 2009, 12:17 AM
Hi,
I have problem with data manipulation in "detail grid".
I have typed DataSet that is binded to MasterDataGrid....that master data grid has 5 columns that are related with 5 fields from one table (MasterTable) but, when i click on Edit in some row, then within <EditFormSettings EditFormType="Template"> i have offered to modify other columns from MasterTable...all key values for masterGrid i suggest within masterGrid_ItemCommand (if e.CommandName == RadGrid.InitInsertCommandName)...
then i have added detailGrid inside that EditFromSettings...
now the problem appear...when i try to use that detailGrid, i can't find it as control in masterGrid, so i can't add row in that grid...even if I add row (with button that has CommandName="PerformInsert"), that command line simply dissapear in grid...like i didn't add it at all...
If i explained it little bit confusy here is some code that will help me to be more precise:
<telerik:RadGrid id="masterGrid" runat="server" AllowMultiRowSelection="false" AutoGenerateEditColumn="true" AutoGenerateColumns="False" DataMember="MasterTable" DataSourceID="someDataSource" GridLines="None" OnItemCommand="masterGrid_ItemCommand"
  <IxMasterTableView DataKeyNames="Col1,Col2,Col3" DataMember="MasterTable" DataSourceID="someDataSource" SelectionItemDisplay="TopAndBottom" AllowSorting="True" AutoGenerateColumns="False" CommandItemDisplay="Top"
    <CommandItemTemplate> some button is here </CommandItemTemplate> 
    <Columns> 
      <telerik:GridBoundColumn DataField="Column1" HeaderText="Column1 name" ReadOnly="True" SortExpression="Column1" UniqueName="Column1" /> 
      and 4 columns more... 
    </Columns> 
    <EditFormSettings EditFormType="Template"
      <FormTemplate> 
        <table> 
          <tr><td>...some TextBox binded columns....</td><tr
          <tr> 
            <td> 
              <telerik:RadGrid ID="detailGrid" runat="server" SelectionEnabled="False" DataSourceID="someDataSource" GridLines="None" DataMember="DetailTable" AutoGenerateColumns="False" AllowSorting="True" OnItemCommand="detailGrid_ItemCommand"
                <IxMasterTableView DataKeyNames="Col1,Col2,Col3,Col4,Col5" DataMember="DetailTable" DataSourceID="someDataSource" SelectionItemDisplay="TopAndBottom" AllowSorting="True" AutoGenerateColumns="False" CommandItemDisplay="Top"
                  <CommandItemTemplate> some button is here </CommandItemTemplate> 
                  <Columns> 
                    <telerik:GridTemplateColumn HeaderText="first" UniqueName="Col1" DataField="Col1" SortExpression="Col1"
                      <ItemTemplate> 
                          ...some link button... 
                      </ItemTemplate> 
                      <EditItemTemplate> 
                        ...some TextBox control... 
                      </EditItemTemplate> 
                    <telerik:GridTemplateColumn> 
                      ...and couple more GridTemplateControls... 
                      ...and last column in grid is something like this... 
                    <telerik:GridTemplateColumn UniqueName="ColumnDelete"
                      <ItemTemplate> 
                         <asp:Button ID="deleteButton" runat="server" Text="Del" CommandName="Delete" /> 
                      </ItemTemplate> 
                      <EditItemTemplate> 
                        <asp:Button ID="addButton" Text="Add" Visible='<%# don't know how to implement as it is in masterGrid %>runat="server" CommandName="PerformInsert" />&nbsp; 
                        <asp:Button ID="updateButton" Text="Update" Visible='<%# same as previous, just on update %>' runat="server" CommandName="Update" />&nbsp; 
                        <asp:Button ID="cancelButton" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel" /> 
                      </EditItemTemplate> 
                      <HeaderStyle /> 
                      <ItemStyle HorizontalAlign="Right" /> 
                    </telerik:GridTemplateColumn> 
                  </Columns> 
              </telerik:RadGrid> 
            </td> 
          </tr> 
          <tr> 
            <td> 
              <asp:Button ID="addMaster" Text="Add" Visible='<%# (masterGrid.IxMasterTableView.IsItemInserted) %>' runat="server" CommandName="PerformInsert" />&nbsp; 
              <asp:Button ID="updateMaster" Text="Update" Visible='<%# !(masterGrid.IxMasterTableView.IsItemInserted) %>' runat="server" CommandName="Update" />&nbsp; 
              <asp:Button ID="cancelMaster" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel" /> 
            </td> 
          </tr> 
        </table> 
      </FormTemplate> 
    </EditFormSettings> 
  </IxMasterTableView> 
</telerik:RadGrid> 

and here is some codebehind for onItemCommand
DataKey oldKeyValues = e.Item.OwnerTableView.DataKeyValues[0]; 
RadGrid detailRadGrid = (sender as RadGrid); 
 
if (e.CommandName == RadGrid.InitInsertCommandName) { 
  detailRadGrid.Save(false); 
  aoGridCommandEventArgs.Canceled = true
  System.Collections.Specialized.ListDictionary newKeyValues = new System.Collections.Specialized.ListDictionary(); 
  if (loDataKey != null) { 
    newKeyValues["Col1"] = oldKeyValues["Col1"]; 
    newKeyValues["Col2"] = oldKeyValues["Col2"]; 
    newKeyValues["Col3"] = oldKeyValues["Col3"]; 
    newKeyValues["Col4"] = oldKeyValues["Col4"]; 
    e.Item.OwnerTableView.InsertItem(newKeyValues); 
  } 

same is for masterGrid...
So, my question would be...how can i add and save value in detailGrid, because this way it is not functioning, and is it possible to filter detailGrid rows to display only rows that are related to masterGrid column that is not in KeyColumns ?
and how can i check which row in rows 33 and 34 (of first code part) will be visible based on logic in rows 46 and 47 ?
Some example of how to use this would be great...i'm stuck here fot 10 days and can't move any further...
Thanks for reply...

4 Answers, 1 is accepted

Sort by
0
Igor
Top achievements
Rank 1
answered on 06 Mar 2009, 02:02 PM
Is it possible that this is so complicated to do ?
0
Tsvetoslav
Telerik team
answered on 10 Mar 2009, 09:31 AM
Hello Igor,

Could you elaborate a bit on the reasons for having another RadGrid in the FormTemplate. Do you need it just for inserting new records in the master grid, or you want also to display additional information related to the master grid? Besides, is your inner grid bound to a different data source, i.e. does it have different columns than the master grid or it has the same columns with some additional ones made visible?

In addition, I could not understand the following question of yours:

"is it possible to filter detailGrid rows to display only rows that are related to masterGrid column that is not in KeyColumns"? Could you provide more information.

Furthermore, please, give more details on the following point in your post:

"and how can i check which row in rows 33 and 34 (of first code part) will be visible based on logic in rows 46 and 47 ?"
 
Thanks.

Awaiting your feedback.

Best Regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Igor
Top achievements
Rank 1
answered on 10 Mar 2009, 02:37 PM
Hi, thanks for answer, and here is more detailed specification of my problem...

I have 2 tables
CREATE TABLE [dbo].[MasterTable]( 
    [Col1] [bigintNOT NULL
    [Col2] [varchar](7) NOT NULL
    [Col3] [varchar](3) NOT NULL
    [Col4] [varchar](3) NULL
    [Col5] [varchar](3) NULL
    [Col6] [varchar](50) NULL
    [Col7] [varchar](50) NULL
    [Col8] [varchar](50) NULL
    [Col9] [varchar](50) NULL
    [Col10] [varchar](50) NULL
    [Col11] [varchar](50) NULL
 CONSTRAINT [PK_MasterTable] PRIMARY KEY CLUSTERED  
    [Col1] ASC
    [Col2] ASC
    [Col3] ASC 
 
CREATE TABLE [dbo].[DetailTable]( 
    [Col1] [bigintNOT NULL
    [Col2] [varchar](7) NOT NULL
    [Col3] [varchar](3) NOT NULL
    [Col4] [varchar](3) NOT NULL
    [Col5] [varchar](3) NOT NULL
    [Col6] [varchar](3) NULL
    [Col7] [varchar](50) NULL
 CONSTRAINT [PK_DetailTable] PRIMARY KEY CLUSTERED  
    [Col1] ASC
    [Col2] ASC
    [Col3] ASC
    [Col4] ASC
    [Col5] ASC 
 


And i need something like this:
One RadGrid is master, and because i have 11 columns, and i don't want to scroll right in grid, I only display couple of those columns in grid list...but, when i insert, or edit data, i use EditFormSettings with FormTemplate to edit all fields that are in MasterTable...
For creating composite keys in MasterTable, i use ItemCommand because i need calculations for key values, and code in ItemCommand in MasterGrid is something like this:
if (e.CommandName == RadGrid.InitInsertCommandName) { 
    (sender as RadGrid).Save(false); 
    e.Canceled = true
 
    System.Collections.Specialized.ListDictionary loNewValues = new System.Collections.Specialized.ListDictionary(); 
 
    loNewValues["Col1"] = Session["Col1CalculatedValue"]; 
    loNewValues["Col2"] = Session["Col2CalculatedValue"]; 
    loNewValues["Col3"] = Session["Col3CalculatedValue"]; 
    loNewValues["Col4"] = Session["Col4CalculatedValue"]; 
 
    e.Item.OwnerTableView.InsertItem(loNewValues); 

For now everything is ok...i succeded to manage this, but problem apperas when i try to put RadGrid in FormTemplate part of masterGrid...
I also need ItemCommand code like this, because i use key values from MasterTable, and one value that is not key (Col5)
loMasterKeyTable = //need data columns for related master row here  
if (e.CommandName == RadGrid.InitInsertCommandName) {  
    (sender as RadGrid).Save(false);  
    e.Canceled = true;  
  
    System.Collections.Specialized.ListDictionary loNewValues = new System.Collections.Specialized.ListDictionary();  
    if (loDataKey != null) {  
        loNewValues["Col1"] = loMasterKeyTable["Col1"];  
        loNewValues["Col2"] = loMasterKeyTable["Col2"];  
        loNewValues["Col3"] = loMasterKeyTable["Col3"];  
        loNewValues["Col4"] = loMasterKeyTable["Col4"];  
        loNewValues["Col5"] = loMasterKeyTable["Col5"];  
    }  
    e.Item.OwnerTableView.InsertItem(loNewValues);  
}

Now...for those 2 grids, i use same SP (same datasource), only it returns 2 tables, so i set different DataMember (I use typed datasets in my application) for every grid.

so my questions would be...
- how can i add rows in inner grid using this ItemCommand that i have ?
- how can i filter rows in inner grid by Col5 that is not in MasterTable keys ?
(that is explanation for this "is it possible to filter detailGrid rows to display only rows that are related to masterGrid column that is not in KeyColumns"? )
- how can i access innser grid from codebehind ?
(i have tried with masterGrid.MasterTableView.FindControl("detailGrid"); and it is not functioning )
- and how can i access it on aspx page, so i can find out if row is inserted or edited, to show or hide button that i don't need ?
(that is explanation for this:"and how can i check which row in rows 33 and 34 (of first code part) will be visible based on logic in rows 46 and 47 ?" numbers 33, 34, 46 and 47 are related to line numbers in HTML code in first post...in lines 33 and 34 is how i decide to show or hide buttons, and i also need that in lines 46, and 47)

Thanks for reply,
Igor
0
Tsvetoslav
Telerik team
answered on 12 Mar 2009, 08:32 AM
Hi Igor,

Thanks for the feedback.

With regards to your first question, I understand that you need to access the data key values of the parent item in the master grid. Here is the code for accessing both the key values and any other values in the parent item. This should be implemented in the ItemCommand event handler for the second grid:

        Hashtable allValues = new Hashtable();  
        GridDataItem item = ((Telerik.Web.UI.GridEditFormItem)(e.Item.OwnerTableView.OwnerGrid.NamingContainer)).ParentItem as GridDataItem;  
        string keyValues = item.KeyValues;  
        item.ExtractValues(allValues); 

Concerning your second question, you should wire up and event handler to the ItemCreated event of the master grid, get a reference to the child grid and set a filter expression for the column in question. For example:

        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)  
        {  
            GridEditFormItem item = e.Item as GridEditFormItem;  
 
            RadGrid detailGrid = item.FindControl("detailGrid"as RadGrid;  
 
            detailGrid.MasterTableView.FilterExpression = "[Col5] <= 7";  
        } 

The code for accessing the inner grid in the ItemCreated, ItemDataBound and ItemCommand events for the master grid is as follows:

        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)  
        {  
            GridEditFormItem item = e.Item as GridEditFormItem;  
 
            RadGrid detailGrid = item.FindControl("detailGrid"as RadGrid;  
 
            detailGrid.MasterTableView.FilterExpression = "[Col5] <= 7";  
        } 

As for your fourth question, I am not sure what exactly you have in mind by "accessing the inner grid on aspx page". However, do keep in mind that the child grid is accessible only when the containing item is in edit mode the child grid is rendered on the page. And the code for that (e.g. in the page pre-render event) becomes:

        foreach (GridDataItem item in masterGrid.Items)  
        {  
            if (item.EditFormItem.IsInEditMode)  
            {   
                RadGrid detailGrid = item.EditFormItem.FindControl("detailGrid"as RadGrid;  
            }  
        }  

Finally, regarding your last question, you should implement your show/hide logic for the buttons in the ItemCreated event for the detail grid. Here is the code for that:

        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)  
        {  
            Button addButton = ((Telerik.Web.UI.GridEditFormItem)(e.Item.OwnerTableView.OwnerGrid.NamingContainer)).FindControl("addMaster"as Button;  
 
            if (!addButton.Visible)  
            {  
                Button addButtonDetail = ((GridEditFormItem)e.Item).FindControl("addButton"as Button;  
                addButtonDetail.Visible=true;  
            }  
        } 

I hope this helps.

Best Regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Igor
Top achievements
Rank 1
Answers by
Igor
Top achievements
Rank 1
Tsvetoslav
Telerik team
Share this question
or