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

Getting selected row index

9 Answers 1499 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sree
Top achievements
Rank 1
Sree asked on 05 Jan 2010, 01:43 PM
Hi
I have a radgridview with multiple rows selected.
How can I get the current selected row index in gridview selectedIndexChanged event?

9 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 05 Jan 2010, 02:20 PM
Hello Sree,

Try out the following code to get the currently selected item:
c#:
 protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        GridDataItem dataItem = (GridDataItem)RadGrid1.SelectedItems[RadGrid1.SelectedItems.Count - 1];  
        string ID = dataItem.GetDataKeyValue("ID").ToString();     
    }  

Thanks
Princy.
0
Radoslav
Telerik team
answered on 05 Jan 2010, 03:38 PM
Hi Sree,

You could get all selected indexes from the grid SelectedIndexes property. It is a collection which holds all selected row indexes. If you want to get the last selected row index you can use the following code snippet for instance:

protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
 int lastSelectedIndex =
  Convert.ToInt32(RadGrid1.SelectedIndexes[RadGrid1.SelectedIndexes.Count - 1]);
}

Additionally I am sending you a simple example demonstrating how to achieve the desired functionality.

I hope this helps.

Best wishes,
Radoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Sree
Top achievements
Rank 1
answered on 06 Jan 2010, 04:47 AM
Hi Radoslav
           Thanks for your  help,but it doesn't solve my problem.I have grid with multiple selected rows(AllowMultiRowSelection="True").In your example you are always taking the selectedindexesitems[0] .It will work since there is only one selected row.In my case I have multiple selected rows and I can select the rows in any order.So please suggest a solution for this.
0
Shinu
Top achievements
Rank 2
answered on 06 Jan 2010, 05:04 AM
Hello Sree,

You can loop through the SelectedItems collection of RadGrid in order to get the corresponding indexes, as shown below.

CS:
 
    protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        foreach (GridDataItem item in RadGrid1.SelectedItems) 
        { 
            string CustID = item.GetDataKeyValue("CustomerID").ToString(); 
            Response.Write(CustID); 
        } 
    } 

-Shinu.
0
Sree
Top achievements
Rank 1
answered on 06 Jan 2010, 05:54 AM
Hi
Thanks to all..At last I have found a solution
In gridview's ItemCommand event we can get the current row index as given below.


protected void gv_ItemCommand(object source, GridCommandEventArgs e)
    {
        Session["RowIndex"] = e.Item.ItemIndex.ToString();
    }


protected void gv_SelectedIndexChanged(object sender, EventArgs e)
    {
     var CurrentSelectedRowIndex=Session["RowIndex"]
}



0
THANGARAI
Top achievements
Rank 1
answered on 04 Apr 2014, 05:32 AM
Hi Princy,

How do I get a gridrow by its index in telerik radgrid without loop.?
0
Princy
Top achievements
Rank 2
answered on 04 Apr 2014, 06:06 AM
Hi Thangarai,

I guess you want to access the currently selected row's index. Please try the following code snippet. If this doesn't help, elaborate on your requirement.

ASPX:
<ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true">
</ClientSettings>

C#:
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
 GridDataItem selectedItem = (GridDataItem)RadGrid1.SelectedItems[0];
 int index = selectedItem.ItemIndex;// Gets the index of currently selected row
}

Thanks,
Princy
0
THANGARAI
Top achievements
Rank 1
answered on 04 Apr 2014, 06:15 AM
Hi Princy,

Thanks for your reply.

I think the above code is for single row select. I have scenario to select multiple row by checking a CHECKBOX,
and I should get all the selected row values where the checkbox is checked.

Note: Please find the attachment.
0
Princy
Top achievements
Rank 2
answered on 07 Apr 2014, 04:08 AM
Hi Thangarai,

Since you want to get all the selected rows, we have to loop through the rows to identify the selected rows and get its index.

C#:
foreach (GridDataItem selectedItem in RadGrid1.SelectedItems)
{       
  int index = selectedItem.ItemIndex;   
}

Thanks,
Princy
Tags
Grid
Asked by
Sree
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Radoslav
Telerik team
Sree
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
THANGARAI
Top achievements
Rank 1
Share this question
or