Get selected row index in grouped grid

1 Answer 169 Views
Grid
Omar
Top achievements
Rank 3
Iron
Iron
Iron
Omar asked on 27 Oct 2023, 12:24 PM | edited on 27 Oct 2023, 12:30 PM

Hi,

I would like to get the selected row index for the grouped grid. Can anyone help me with this?

 


<telerik:RadGrid ID="Grid_logic" runat="server" AutoGenerateColumns="false" Font-Size="Small" Width="600px" Skin="Default" ShowGroupPanel="false" OnSelectedIndexChanged="Grid_logic_SelectedIndexChanged">
    <GroupingSettings CollapseAllTooltip="Collapse all groups"  />
       <MasterTableView ClientDataKeyNames="logicID"  DataKeyNames="logicID"  ShowGroupFooter="false" ShowHeader="false" GroupsDefaultExpanded="true" GroupLoadMode="Client">
          <Columns>
                                                    
            <telerik:GridBoundColumn DataField="logicID" FilterControlAltText="Filter column column" HeaderText="" UniqueName="logicID"  Visible="true" >
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Answer" FilterControlAltText="Filter column column" HeaderText="Answer" UniqueName="Answer">
            </telerik:GridBoundColumn>
           <telerik:GridBoundColumn DataField="IncludedPages" FilterControlAltText="Filter column column" HeaderText="Included Pages" UniqueName="IncludedPages">
           </telerik:GridBoundColumn>
        </Columns>

         <GroupByExpressions>
            <telerik:GridGroupByExpression>
                <SelectFields>
                     <telerik:GridGroupByField FieldAlias="Answer" FieldName="Answer" HeaderText="" />
                     <telerik:GridGroupByField FieldName="IncludedPages" HeaderText="IncludedPages" />
                </SelectFields>
      <GroupByFields>
        <telerik:GridGroupByField FieldAlias="Page" FieldName="Page" FormatString="" HeaderText="" />
       <telerik:GridGroupByField FieldAlias="Question" FieldName="Question" FormatString="" HeaderText="" />
     </GroupByFields>
  </telerik:GridGroupByExpression>
 </GroupByExpressions>
 <GroupHeaderTemplate>
    <table>
        <tr style="width: 100%">
        <td style="vertical-align: central">
        <asp:Label ID="Page" runat="server" Text='<%# Eval("Page") %>' ></asp:Label>
       </td>
     <td style="vertical-align: central">
     <asp:Label ID="Question" runat="server" Text='<%# Eval("Question") %>'></asp:Label>
      </td>
    </tr>
  </table>
  </GroupHeaderTemplate>
 </MasterTableView>
                                            
 <ClientSettings AllowDragToGroup="true" >
 <Selecting AllowRowSelect="true"></Selecting>
 <ClientEvents OnRowContextMenu="RowContextMenu" />
 </ClientSettings>
</telerik:RadGrid>

 

An example of my field attempts.


protected void Grid_logic_SelectedIndexChanged(object sender, EventArgs e)
        {
            int x = Grid_logic.SelectedItems.Count;
            if (Grid_logic.SelectedItems.Count > 0 && Grid_logic.SelectedItems[0].OwnerTableView.DataKeyValues.Count > 0)
            {
                var z = Grid_logic.SelectedItems[0].OwnerTableView.DataKeyValues[Grid_logic.SelectedItems[0].ItemIndex]["logicID"];
            }
        }

Thanks,

 

1 Answer, 1 is accepted

Sort by
0
Vasko
Telerik team
answered on 01 Nov 2023, 11:53 AM | edited on 01 Nov 2023, 12:00 PM

Hello Omar,

Based on the shared code snippet, my understanding is that you want to access the DataKeyValue of the currently selected row.

If that is the case, there is no need to use the item index at all, but access the DataKeyValue directly from the selected item:

    protected void Grid_logic_SelectedIndexChanged(object sender, EventArgs e)
    {
        var grid = (RadGrid)sender;

        if (grid.SelectedItems.Count > 0)
        {
            GridDataItem selectedItem = grid.SelectedItems[0] as GridDataItem;

            int dataKeyValue = (int)selectedItem.GetDataKeyValue("YourIDGoesHere"); // The field set in the DataKeyValue fields of the MasterTable.
        }
    }

Otherwise, the index of the item is stored in the itemIndex and itemIndexHierarchical (in the case of a hierarchical structure) properties.

I hope this will help you out.

Kind regards,
Vasko
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources
Tags
Grid
Asked by
Omar
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Vasko
Telerik team
Share this question
or