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

Get Selected Row and Row Data

4 Answers 317 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Christopher Ronak
Top achievements
Rank 1
Christopher Ronak asked on 10 Jun 2010, 09:04 PM
Can you please tell me how to get the selected row of a grid? NOT via the onSelected event, but from an outside source. This doesn't seem to be documented anywhere. I tried a similar syntax as the treeview, but that didn't work.
var gridData = $('#TelerikGrid').data('tGrid');
var selectedItem = gridData.find('.t-state-selected').closest('.t-item');
Then obtain the value of the various columns from the row. I'm guessing looks like this:
row.cells["COL1"].innerHTML
etc.

Thanks for your help.

4 Answers, 1 is accepted

Sort by
0
Paul
Top achievements
Rank 1
answered on 23 Sep 2011, 01:45 PM
Did you ever find the answer to this question? I need to do the same.

Cheers
Paul
0
ART
Top achievements
Rank 1
answered on 23 Sep 2011, 06:33 PM
I've also been trying to do this using the following code below.  When it gets to the grid.dataItem(tr) line, the javascript engine throws an error.  I've checked and tr is not null.
var tr = $("#MyGrid tbody tr.t-state-selected");
var grid = $("#MyGrid").data("tGrid");
var data = grid.dataItem(tr);
0
Paul
Top achievements
Rank 1
answered on 23 Sep 2011, 07:06 PM
I sorted it myself in the end

On my grid definition I added this column

            col.Bound(o => o.ComplianceId).Hidden(true).HtmlAttributes(new { id = "id" });

which is the primary key of my data item.

Then in JavaScript I use this to obtain the primary key value of the selected row

                var id = $('#grdCompliance tr.t-state-selected #id').html();

You can do a test on id as follows to determine if in fact a row has been selected. It will be null if not.

                if (id == null) {
                }
0
Dimo
Telerik team
answered on 26 Sep 2011, 08:37 AM
Hello Art,

You are probably using server-side binding. In this case the data property of the Grid object is undefined and the dataItem cannot be retrieved by using the API. You can obtain the values of the selected row with standard DOM operations, or switch to AJAX binding.

Best wishes,
Dimo
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
Tags
Grid
Asked by
Christopher Ronak
Top achievements
Rank 1
Answers by
Paul
Top achievements
Rank 1
ART
Top achievements
Rank 1
Dimo
Telerik team
Share this question
or