Unbound datagridview sorting

  • Thread starter Thread starter guyinkalamazoo3
  • Start date Start date
G

guyinkalamazoo3

Guest
I have a datagridview that I am binding to a linq table. The resulting table is five columns and I am hiding two (columns that are not needed like ID). The users want to be able to sort clicking on a column header. I have this code and when I run it, I get an error about and unbound datagrid cannot be sorted.

System.InvalidOperationException: 'DataGridView control must be bound to an IBindingList object to be sorted.'

The code is and it breaks on the .Sort function



Dim columnIdx As Integer = e.ColumnIndex
Dim selectedColumnName As String = dgvAuditResults.Columns(columnIdx).HeaderText
If dgvAuditResults.Columns(columnIdx).HeaderCell.SortGlyphDirection = SortOrder.None Or
dgvAuditResults.Columns(columnIdx).HeaderCell.SortGlyphDirection = SortOrder.Descending Then
dgvAuditResults.Columns(columnIdx).HeaderCell.SortGlyphDirection = SortOrder.Ascending
dgvAuditResults.Sort(dgvAuditResults.Columns(columnIdx), ComponentModel.ListSortDirection.Ascending)
Else
dgvAuditResults.Columns(columnIdx).HeaderCell.SortGlyphDirection = SortOrder.Descending
dgvAuditResults.Sort(dgvAuditResults.Columns(columnIdx), ComponentModel.ListSortDirection.Descending)
End If
My thought is having to rerun a new sorted linq table based on the selection, but I also need to take into account if it is ASC or DESC. Is there a simpler way to do this? Thanks




Brad Allison

Continue reading...
 
Back
Top