How to select row in Telerik RadGrid?

25,027

Solution 1

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.

Solution 2

Try this. This may help you.

STEP 1:Add one radiobutton column in the radgrid

STEP 1: Get the primary key of selected row in the radgrid.

    int primaryKey =0;
    RadioButton radioButton;  
    for (int i = 0; i < RadGrid1.Items.Count; i++)  
    {  
        radioButton = RadGrid1.Items[i].FindControl("rdSelect") as RadioButton;
        If (radioButton.Checked)
        { 
           primaryKey = RadGrid1.MasterTableView.Items[e.Item.ItemIndex]["ID"].Text;
        }
    }


Line in the if condition will be used to get the fields from the selected row just by changing the fields datakey name i.e. changing "ID" to other field

Read this article for more details...

http://codedotnets.blogspot.in/2012/01/get-primary-key-selected-radiobutton.html

Solution 3

This should get you going. It is a solution straight from Telerik: Retrieving primary key values for selected items

Share:
25,027
ozkank
Author by

ozkank

Updated on July 10, 2022

Comments

  • ozkank
    ozkank almost 2 years

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

  • S.L. Barth
    S.L. Barth over 11 years
    Welcome to StackOverflow! Please add some explanation in the answer. That way your answer will still be useful if the link dies.
  • S.L. Barth
    S.L. Barth over 11 years
    You can edit your posts on this site. In this case, I have edited your comment into the answer - other people can edit your posts here too.