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

Accessing selected row cell values on post back

2 Answers 245 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tahir Ahmed
Top achievements
Rank 1
Tahir Ahmed asked on 01 Mar 2012, 08:33 AM
Hello,

I am trying to access a cell value on post back when ever the user will click command button (select) in RadGrid. I have a command button with name select and this is what I am doing in code behind:

    protected void leadsGrid_ItemCommand(object sender, GridCommandEventArgs e)
    {
        GridDataItem myDataItem = (GridDataItem)leadsGrid.SelectedItems[0].DataItem;
        Response.Write(myDataItem["lead_name"].Text);
    }

but whenever I click on select button it gives me

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


I supposed its occurring because SelectedItems[0] does not have anything but I don't know why it does not have anything as pressing the select command should select the row ?

Please tell me what I am doing wrong ? or a correct way to access cell values of selected row. I am not working on multiple rows so I only want to access cell values of one select row. I have experience with normal ASP.net GridView but I am fairly new with RadGrid so I apologize if this is a basic question.

Thanks.

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 01 Mar 2012, 08:55 AM
Hello,

Try the following code in SelectedIndexChanged event of RadGrid.
C#:
void grid_SelectedIndexChanged(object sender, EventArgs e)
{
  foreach (GridDataItem item in grid.SelectedItems)
  {
     string txt1 = item["lead_name"].Text;
  }
}

-Shinu.
0
Tahir Ahmed
Top achievements
Rank 1
answered on 01 Mar 2012, 09:08 AM
Thank you shinu, worked like a charm.

I didn't know that select command will work with SelectedIndexChange as it works in normal GridView because when you double click on GridView it automatically creates an event handler for SelectedIndexChange where as double clicking RadGrid creates an event handler for NeedDataSource so this created a confusion.

Thank you for your help.
Tags
Grid
Asked by
Tahir Ahmed
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Tahir Ahmed
Top achievements
Rank 1
Share this question
or