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

Kendo UI grid set selected row

1 Answer 460 Views
Grid
This is a migrated thread and some comments may be shown as answers.
santhosh
Top achievements
Rank 1
santhosh asked on 14 Nov 2013, 03:26 PM
Hi,

 I have list of users (List<User>) binded to a grid. I have a currently logged in user information in ViewData[User]. 
What I want is if a grid contains a user which is in ViewData. I want that user to be selected as soon as grid loads data. How can I do this validation ? How can I get currently selected row ?


@(Html.Kendo().Grid<KendoGridAjaxBinding.Models.User>()
.Name("grid")
.DataSource(dataSource => dataSource  
.Ajax() // Specify that ajax binding is used
.Read(read => read.Action("User_Read", "Home")) // Set the action method which will return the data in JSON format
)
.Columns(columns =>
{
 
columns.Bound(user=> user.UserID); 
columns.Bound(user=> user.UserName); 
columns.Bound(user=> user.City);;
})
.Pageable() // Enable paging
.Sortable() // Enable sorting
)

1 Answer, 1 is accepted

Sort by
0
Ignacio
Top achievements
Rank 1
answered on 14 Nov 2013, 10:18 PM
I would suggest you store a primitive (like Username) instead of the whole User object on your ViewBag.

If you do this it shouldnt be hard to:

  • Store the Username on a javascript variable. (var name = "@ViewBag.Username";)
  • Subscribe to the DataBound event on the grid.
  • Use jQuery to find the row to select based on the data of a cell or a data attribute you add to it.
  • Use the select method on the grid on that found row to select it.
Tags
Grid
Asked by
santhosh
Top achievements
Rank 1
Answers by
Ignacio
Top achievements
Rank 1
Share this question
or