简体   繁体   中英

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?

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.

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 ie changing "ID" to other field

Read this article for more details...

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM