How do I make a DataGridView sort-able when using Entity Framework to pull data from a database?
I'm putting the query into the DataSource of the DataGridView.
Dim Query = (From t In DB.interview_task Where t.CONTROL = CONTROL And t.CLIENTCODE = CLIENTCODE Order By t.StartDate Descending
Select t.ID, t.CONTROL, t.CLIENTCODE, t.TaskType, t.Title, t.StartDate, t.DueUserName, Status = If(t.CompleteDate Is Nothing, "In Progress", "Completed")).ToList
dgvTaskList.DataSource = Query
The only way to load data into a DGV is by turning it into a .List but this makes the grid unsortable.
The examples I'm seeing on Google are outdated or really complicated. This feels like something that should be simple. I was dumping the query into a DataTable but I get back Time which isn't on the column.
So how do I put an EF Query on a DGV and make it sort-able?
Update:
So I was able to get it to work using Karen's Answer, I did the following;
Public Sub Load_TaskList()
Using DB As New wotcDB
Dim Query2 = From t In DB.interview_task Where t.CONTROL = CONTROL And t.CLIENTCODE = CLIENTCODE Order By t.StartDate Descending
Select New TaskList With {.ID = t.ID,
.CONTROL = t.CONTROL,
.CLIENTCODE = t.CLIENTCODE,
.TaskType = t.TaskType,
.Title = t.Title,
.StartDate = t.StartDate,
.Status = If(t.CompleteDate Is 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 As Integer
Public Property CONTROL As Integer
Public Property CLIENTCODE As String
Public Property TaskType As String
Public Property Title As String
Public Property StartDate As Date?
Public Property DueUserName As String
Public Property Status As String
End Class
So for another question. Is it possible to use this sorting method without having to declare TaskList?
Sort(...)? Just looking at the documentation for DataGridView, I see aColumnHeaderMouseClickEvent. Why not listen to it and callSortfor that column? That seems to be the simplest. msdn.microsoft.com/en-us/library/0868ft3z(v=vs.110).aspx