vb.net - Entity Framework: How to make DataGridView Sortable? -
how make datagridview sort-able when using entity framework pull data database?
i'm putting query datasource of datagridview.
dim query = (from t in db.interview_task t.control = control , t.clientcode = clientcode order t.startdate descending select t.id, t.control, t.clientcode, t.tasktype, t.title, t.startdate, t.dueusername, status = if(t.completedate nothing, "in progress", "completed")).tolist dgvtasklist.datasource = query
the way load data dgv turning .list makes grid unsortable.
the examples i'm seeing on google outdated or complicated. feels should simple. dumping query datatable time isn't on column.
so how put ef query on dgv , make sort-able?
update:
so able work using karen's answer, did following;
public sub load_tasklist() using db new wotcdb dim query2 = t in db.interview_task t.control = control , t.clientcode = clientcode order t.startdate descending select new tasklist {.id = t.id, .control = t.control, .clientcode = t.clientcode, .tasktype = t.tasktype, .title = t.title, .startdate = t.startdate, .status = if(t.completedate nothing, "in progress", "completed")} dgvtasklist.datasource = new wotc_common.sortablebindinglist(of tasklist)(query2.tolist) end using dgvtasklist.columns("id").visible = false dgvtasklist.columns("control").visible = false dgvtasklist.columns("clientcode").visible = false end sub class tasklist public property id integer public property control integer public property clientcode string public property tasktype string public property title string public property startdate date? public property dueusername string public property status string end class
so question. possible use sorting method without having declare tasklist?
use sortablebindinglist. create , set assign bindingsource , assign bindingsource datagridview. sorry example (and easy follow) in c# in msdn code sample did ef6 in windows forms.
the sortablebindinglist https://code.msdn.microsoft.com/windowsdesktop/generic-sortable-binding-47cac3cc
my code sample, https://code.msdn.microsoft.com/entity-framework-in-764fa5ba
download class in first link, @ code in form1, load event blcustomers set entity customers sortablebindinglist set bscustomers bindingsource , bscustomers becomes datasource datagridview. if need in vb.net can put 1 later, vs2015 on machine updating.
update here data entity using simple select , use class strong type data. bindingsource optional functionality provides. note in button1 cast current property of bindingsource democlass , 2 properties.
public class form1 private bscustomers new bindingsource private sub form1_load(sender object, e eventargs) handles mybase.load using entity new demoentities dim results = entity _ .customers _ .select(function(items) new democlass { .id = items.id, .lastname = items.lastname } ).tolist bscustomers.datasource = new sortablebindinglist(of democlass)(results) datagridview1.datasource = bscustomers end using end sub private sub button1_click(sender object, e eventargs) handles button1.click dim lastname string = ctype(bscustomers.current, democlass).lastname dim identifier integer = ctype(bscustomers.current, democlass).id messagebox.show($"id: {identifier} lastname: {lastname}") end sub end class class democlass public property id integer public property lastname string end class
note syntax messagebox content vs2015, lower version use string.format.
Comments
Post a Comment