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

How to select a row on serverside based on the datakey

4 Answers 443 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Som
Top achievements
Rank 1
Som asked on 15 Jan 2010, 05:33 PM
I have been digging through the help and could not find a solution.

I need to select a row server side based on the ID. The following piece of code does not find the item:

if (Session["selID"] != null)
            {
                GridDataItem item = RadGrid1.MasterTableView.FindItemByKeyValue("Container_ID", Session["selID"].ToString());
                item.Selected = true;
                Session["selID"] = null;
            }

So I'm using the following method 
if (Session["selID"] != null)
            {
                foreach (GridItem item in RadGrid1.MasterTableView.Items)
                {
                    if (item is GridDataItem)
                    {
                        GridDataItem dataItem = (GridDataItem)item;
                        if (Session["selID"].ToString().Equals(dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["Container_ID"].ToString()))
                        {
                            dataItem.Selected = true;
                            Session["selID"] = null;
                            break;
                        }
                    }
                }
            }

Somehow I feel that there has to be a better way to do it. I will appreciate if you could please suggest the optimum way.

Thanks
Som

4 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 15 Jan 2010, 05:44 PM
Hello Som,

I think that you can utilize the approach presented in this help topic to achieve your goal. Review the information and code snippets from it for more details.
 
Kind regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Som
Top achievements
Rank 1
answered on 15 Jan 2010, 06:28 PM
Thanks Sebastian for your help.

My current solution is based on the approach you mentioned. I was just curious if there is any other way.

Thanks again,
Som
0
James
Top achievements
Rank 1
answered on 10 Jun 2010, 07:33 PM
Som I was able to make it work by doing this:
GridDataItem item = grid.MasterTableView.FindItemByKeyValue("NameOfMyDataField", myValue);  
if (item != null)  
{  
   item.Selected = true;  

In the aspx:
        <telerik:RadGrid ID="grid" runat="server">  
            <MasterTableView DataKeyNames="NameOfMyDataField">  
                <Columns> 
                    <telerik:GridBoundColumn DataField="NameOfMyDataField" UniqueName="colNameOfMyDataField">  
                    </telerik:GridBoundColumn> 
                    .... 

The important part is setting the DataKeyNames and making sure it matches to the pk in your datasource you are binding to.  In my case I was binding to a List<> of custom classes.

Hope this helps.
0
NEX
Top achievements
Rank 1
answered on 27 Apr 2014, 06:42 PM
Didn't work for me but the for loop solution did. Thanks
Tags
Grid
Asked by
Som
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Som
Top achievements
Rank 1
James
Top achievements
Rank 1
NEX
Top achievements
Rank 1
Share this question
or