How to select row in Telerik RadGrid?

ozkank picture ozkank · Dec 6, 2011 · Viewed 24.6k times · Source

When user select a row in Telerik Rad Grid, i want to take fields in this row. how to do this?

Answer

KreepN picture KreepN · Dec 6, 2011

It's a little tricky, but easy after you've done it once.

Step 1.

Go to the Radgrid itself and edit the field DataKeyNames="" (under MasterTableView) and add the datafield you are pulling:

<MasterTableView ... DataKeyNames="ColumnNameFromSqlGoesHere">

Step 2.

Decide how you are going to grab the values, on Row Change (SelectedIndexChanged) or on a buttong press with a command attached to it (ItemCommand).

If row change, per your question:

protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
    var z = RadGrid1.SelectedItems[0].OwnerTableView.DataKeyValues[RadGrid1.SelectedItems[0].ItemIndex]["ColumnNameFromSqlGoesHere"];
}

This will assign the variable "z" to the value of the column you have chosen (ColumnNameFromSqlGoesHere) at that given row.

If you wish to select multiple variables every time you change row you need to add all the values you wish to select under the DataKeyNames=" ". (Seperated by commas). You would then fetch each value via the code seen in the SelectedIndexChanged method:

var a = RadGrid1.SelectedItems[0].OwnerTableView.DataKeyValues[RadGrid1.SelectedItems[0].ItemIndex]["SecondColumnGoesHere"];

var b = RadGrid1.SelectedItems[0].OwnerTableView.DataKeyValues[RadGrid1.SelectedItems[0].ItemIndex]["ThirdColumnGoesHere"];

Etc... You get the idea.