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

Get selected row in Detail table

7 Answers 503 Views
Grid
This is a migrated thread and some comments may be shown as answers.
L
Top achievements
Rank 1
L asked on 31 Aug 2012, 08:59 PM
Hi Telerik team,

I have a Hierarchical grid implemented as follows
<telerik:RadGrid ID="radGridMaster" runat="server" AutoGenerateColumns="false" OnDetailTableDataBind="radGridMaster_OnDetailTableDataBind">
    <PagerStyle Mode="NextPrevAndNumeric" Position="TopAndBottom" />
    <ClientSettings>
        <Selecting AllowRowSelect="true" UseClientSelectColumnOnly="true"/>
    </ClientSettings>
    <MasterTableView AutoGenerateColumns="false" DataKeyNames="Name1" Name="Master">
        <DetailTables>
            <telerik:GridTableView Name="Detail">
                <Columns>
                    <telerik:GridBoundColumn DataField="SubName1"  SortExpression="SubName1" UniqueName="SubName1">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="SubName2" SortExpression="SubName2" UniqueName="SubName2">
                    </telerik:GridBoundColumn>
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
        <Columns>
            <telerik:GridBoundColumn DataField="Name1"  SortExpression="Name1" UniqueName="Name1">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Name2" SortExpression="Name2" UniqueName="Name2">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
I have two buttons above the grid two move items up and down. I was able to move up and down the rows inside the Master Grid. I need to achieve the same behavior in the Details Grid.
I need to know how to access:
- The selected element from the detail grid. In Master grid I got as follow: radGridMaster.SelectedItems[0]
- The parent row of the Detail grid in the Master grid.
- The DataSource of the Detail grid. In the Master grid is radGridMaster.DataSource

Thanks in advance,

L

7 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 01 Sep 2012, 09:34 AM
Hello,

Please try with below code snippet.
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnItemCommand="RadGrid1_ItemCommand"
          OnItemDataBound="RadGrid1_ItemDataBound" OnNeedDataSource="RadGrid1_NeedDataSource"
          OnItemCreated="RadGrid1_ItemCreated" AllowMultiRowSelection="true">
          <MasterTableView  Name="Parent">
              <Columns>
                  <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                  </telerik:GridBoundColumn>
                  <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                  </telerik:GridBoundColumn>
              </Columns>
              <DetailTables>
                  <telerik:GridTableView Name="Child">
                      <Columns>
                          <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                          </telerik:GridBoundColumn>
                          <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                          </telerik:GridBoundColumn>
                      </Columns>
                  </telerik:GridTableView>
              </DetailTables>
          </MasterTableView>
          <ClientSettings>
              <Selecting AllowRowSelect="true" />
          </ClientSettings>
      </telerik:RadGrid>
      <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e)
   {
       foreach (GridDataItem item in RadGrid1.SelectedItems)
       {
           if (item.OwnerTableView.Name == "Parent")
           {
               // Parent item
               // Access current row column
               string strID = item["ID"].Text;
           }
           else if (item.OwnerTableView.Name == "Child")
           {
               // Child item
               // Access current row column
               string strID = item["ID"].Text;
               // Access Current row's parent
               string strPID = item.OwnerTableView.ParentItem["ID"].Text;
           }
       }
   }


Thanks,
Jayesh Goyani
0
L
Top achievements
Rank 1
answered on 03 Sep 2012, 07:43 PM
Thanks Jayesh, that was exactly what I was looking for.

I have another question, how to access to the datasources of the child table inside that Button_Click1 method that you put me as an example?

Thanks in advance.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 04 Sep 2012, 05:28 AM
Hello,

Please try with below code snippet.

protected void Button1_Click(object sender, EventArgs e)
    {
 
        Button btnChild = sender as Button;
        GridDataItem item = btnChild.NamingContainer as GridDataItem;
        string strID = item["ID"].Text;
        // Access Current row's parent
        string strPID = item.OwnerTableView.ParentItem["ID"].Text;
      
    }


Thanks,
Jayesh Goyani
0
L
Top achievements
Rank 1
answered on 04 Sep 2012, 12:30 PM
Hi Jayesh,

Thanks for your quick answer but that was not what I was looking for.
I need to access to the datasource of the detail table inside the Button1_Click the same way I do in the OnDetailTabeDataBind event

protected void Button1_Click(object sender, EventArgs e)
        {
            ???.DetailTableView.DataSource = new List<string>();
        }

Thanks in advance,

0
Jayesh Goyani
Top achievements
Rank 2
answered on 04 Sep 2012, 01:09 PM
Hello,

foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            // Access parent grid here
            if (item.HasChildItems)
            {
                foreach (GridDataItem citem in item.ChildItem.NestedTableViews[0].Items)
                {
                    item.ChildItem.NestedTableViews[0].DataSource = null;
                }
            }
        }


Let me know if any concern.

Thanks,
Jayesh Goyani
0
L
Top achievements
Rank 1
answered on 04 Sep 2012, 01:15 PM
Hi Jayesh, 

Your solution worked only for the first child table. 
To put you an example,

Master Table looks like this
Row 1
Row 2
Row 3

If I expanded Row 1, it will look like
Row1
SubRow11
SubRow12
SubRow13

Based on your solution, I was able to bind only the FIRST child table, the one that contains SubRow11, SubRow12 and SubRow13 after I moved any of them up down. However,
If I expanded Row 2
Row2
SubRow21
SubRow22
SubRow23 
and try to move the elements up and down, I'm not able to rebind that child table that contains SubRow21, SubRow22 and SubRow23 and all subsequents child table.

Any ideas?


0
L
Top achievements
Rank 1
answered on 04 Sep 2012, 02:19 PM
Thanks Jayesh for your support, I have found the answer of the question I just asked you. Here it is for future references.

foreach (GridDataItem item in RadGrid1.SelectedItems)
{
  if (item.OwnerTableView.Name == "Master")
{
RadGrid1.DataSource = new List<string>();
}
else
{
if (item.OwnerTableView.Name == "Child")
{
item.OwnerTableView.DataSource = new List<string>();
}
}
Tags
Grid
Asked by
L
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
L
Top achievements
Rank 1
Share this question
or