ozkank
ozkank

Reputation: 1462

How to select row in Telerik RadGrid?

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

Upvotes: 3

Views: 26338

Answers (3)

suyog
suyog

Reputation: 29

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

Upvotes: 2

KreepN
KreepN

Reputation: 8598

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.

Upvotes: 10

NakedBrunch
NakedBrunch

Reputation: 49423

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

Upvotes: 2

Related Questions