Using datasource to sort grid

1 Answer 188 Views
Grid
Eric
Top achievements
Rank 1
Eric asked on 02 Mar 2023, 11:10 PM | edited on 03 Mar 2023, 08:25 PM

Greetings,

 

Im having trouble getting my grid to order the way i want.  I thought that you could use .DataSource to setup sorting but nothing im seeing come up fits my situation.  I want to sort my "haslinked" individuals to the bottom of the grid.  Thanks!

 

Edit:  i just realized my kendo grid is model vs server side.  Thats what i dont get a .sort after datasource.  Any idea how to sort a grid with this new finding?  I believe it essentially means im trying to organize a grid with dynamic content.

 


@(Html.Kendo().Grid(Model)
      .Name("grid")
      .Columns(columns =>
      {
          columns.Bound(model => model.UserName).Title("UserName");
          columns.Bound(model => model.RoleList).Title("Role(s)");
          columns.Bound(model => model.HasLinkedDistilleries).Title("HasLink");
          columns.Bound(b => b.Id).Template(@<text>@Html.ActionLink("Delete", "Delete", "Account", new { id = item.Id }, null)</text>).Title("");
          columns.Bound(b => b.Id).Template(@<text>@Html.ActionLink("Edit", "Edit", "Account", new { id = item.Id }, null)</text>).Title("");
          columns.Bound(b => b.Id).Template(@<text>@Html.ActionLink("Permissions", "Index", "UserPermissions", new { id = item.Id }, null)</text>).Title("").HtmlAttributes(new {@class="" });
      })
       .ToolBar(tools =>
            {
            tools.Template(@<text>@ToolbarTemplate()</text>);
       })
      .Sortable() // Enable sorting
      .DataSource()d => d......Heres where im having trouble.)
      .Filterable()
      .Scrollable()

1 Answer, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 07 Mar 2023, 12:22 PM

Hello Eric,

I've tested the scenario and sorting works even though the Grid is bound to data that comes in the model. Here's my declaration of the Grid:

@model IEnumerable<TelerikMvcApp93.Models.Person>

@(Html.Kendo().Grid(Model)
	.Name("grid")
	.Columns(columns =>
	{
		columns.Bound(c => c.PersonID).Width(200);
		columns.Bound(c => c.Name).Width(200);
		columns.Bound(c => c.HasLinkedDistilleries).Title("HasLink");
		columns.Bound(c => c.BirthDate).Format("{0:dd/MM/yyyy}").Width(200);
	})
	.DataSource(dataSource => dataSource
		.Ajax()
		.Sort(sort => sort.Add(field => field.HasLinkedDistilleries).Ascending())
		.Model(model => model.Id(m => m.PersonID))
	)
	.Sortable()
)

Note the DataSource configuration. You can specify Ascending or Descending sort order.

I'm not sure what is the type of the HasLinkedDistilleries field in your model, but I used bool and it works as expected. Let me know in case I am missing something. 

Regards,
Ivan Danchev
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
Eric
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
Share this question
or