RadGrid for ASP.NET

Multi-row edit/selection only for master table in hierarchical grid Send comments on this topic.
Selecting grid items > How-to > Multi-row edit/selection only for master table in hierarchical grid

Glossary Item Box

If you enable multi-row edit/selection for Telerik RadGrid (through the AllowMultiRowEdit/AllowMultiRowSelection properties) these settings will be inherited for the child tables in the grid hierarchy. In case you want to disable these features for the inner tables, you will need to do that with several lines of custom client/server code.

To disable the multi-row edit for detail tables:

  1. Wire the ItemCommand event of the grid
  2. Check whether e.CommandName is RadGrid.EditCommandName and Not e.Item.OwnerTableView Is e.Item.OwnerTableView.OwnerGrid.MasterTableView
  3. Traverse the items in the e.Item.OwnerTableView.Items collection and set their Edit property to false

To disable the multi-row selection for detail tables:

  1. Hook the RowSelecting client event of the grid
  2. Verify that the clicked table is child table (referencing a column from that detail table)
  3. See whether the SelectedRows collection length is equal to 1
  4. Iterate through the rows in the inner table and deselect them if selected previously

The code below illustrates a sample case (note that it is designed to work with two level hierarchy but can be extended for n-level hierarchy as well):

ASPX/ASCX Copy Code
<script type="text/javascript">
           
function RowSelecting(rowObject)
           {
             if(this.Columns[1].UniqueName == "OrderID")
             {
                if(this.SelectedRows.length == 1)
                {
                  for (var i = 0; i
< this.Rows.length; i++)
                  {
                    if(this.Rows[i].Selected)
                    {
                      this.SelectRow(this.Rows[i].Control, false);
                    }
                  }
                }
             }
           }
<
/script>
<
rad:RadGrid ID="RadGrid1" AllowMultiRowEdit="true" AllowSorting="true" DataSourceID="SqlDataSource1"
   
runat="server" AllowPaging="true" PageSize="3" AllowMultiRowSelection="true">
               
<MasterTableView DataKeyNames="CustomerID">
                   
<Columns>
                       
<rad:GridEditCommandColumn />
                   
</Columns>
                   
<DetailTables>
                       
<rad:GridTableView DataKeyNames="OrderID,CustomerID" DataSourceID="SqlDataSource2">
                           
<ParentTableRelation>
                               
<rad:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID" />
                           
</ParentTableRelation>
                           
<Columns>
                               
<rad:GridEditCommandColumn />
                           
</Columns>
                       
</rad:GridTableView>
                   
</DetailTables>
               
</MasterTableView>
               
<ClientSettings>
               
<Selecting AllowRowSelect="true" EnableDragToSelectRows="true" />
               
<ClientEvents OnRowSelecting="RowSelecting" />
               
</ClientSettings>
           
</rad:RadGrid>
           
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<$ ConnectionStrings>"
               
SelectCommand="SELECT * FROM [Customers]"></asp:SqlDataSource>
           
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<$ ConnectionStrings>"
               
SelectCommand="SELECT * FROM [Orders] WHERE ([CustomerID] = @CustomerID)">
               
<SelectParameters>
                   
<asp:SessionParameter Name="CustomerID" SessionField="CustomerID" Type="String" />
               
</SelectParameters>
           
</asp:SqlDataSource>

 

C# Copy Code
protected void RadGrid1_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
{
       
if (e.CommandName == RadGrid.EditCommandName && e.Item.OwnerTableView != e.Item.OwnerTableView.OwnerGrid.MasterTableView)
       {
           GridItem item;

           
foreach (item in e.Item.OwnerTableView.Items)
           {
               item.Edit = false;
           }
       }
}
VB.NET Copy Code
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.ItemCommand
        If e.CommandName = RadGrid.EditCommandName AndAlso Not e.Item.OwnerTableView Is e.Item.OwnerTableView.OwnerGrid.MasterTableView
        Then
            Dim item As GridItem
            For Each item In e.Item.OwnerTableView.Items
                item.Edit = False
            Next
        End If
End Sub