G
Gunner359
Guest
I have a DataGridView that is filled with data from a dataset. I have a Windows Form button that when clicked, runs an SQL that sorts the data and refills the grid. The following is the code:
strSQL = "SELECT * FROM AccessTable WHERE ID = 'P' OR ID = 'PD' OR ID = 'PN' ORDER BY [autoseq], [row_seq]"
Dim cn as New OleDbConnection(My.Settings.ConnString)
cn.Open()
Using daDB As New OleDbDataAdapter(strSQL, cn)
ds.Clear()
daDB.Fill(ds)
End Using
cn.Close()
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.Refresh
All works well. The data displays in the grid like so:
autoseq ID Description Code
60 P Description1 Code1 .....
61 PD Description2 Code2 .....
62 PN Description3 Code3 .....
63 P Description4 Code4 .....
...
When I click on the Description column heading, the grid sorts by Description, either ascending or descending. No problem. When I click on the form button that executes the above code, the DataGridView continues to display in description ascending or descending order. The data in the DataGridView does not display sorted by autoseq. I tried clearing DataGridView1 in a variety of ways but that didn't work.
I stopped the code at DataGridView1.Refresh. I used the Immediate window to see what was in some of the dataset records and they appeared to be in the desired sorted order. That is, ds.Tables(0).rows(0).item(0) was 60, ds.Tables(0).rows(1).item(0) was 61, ... but yet the Grid still displayed the data in Description asc or desc order with the appropriate glyph.
I then researched if I could somehow turn off SelectedColumn sort. I tried the following code and tried it before the cn.Open() and just before binding the datasource to the dataset:
If DataGridView1.SortedColumn IsNot Nothing Then
DataGridView1.Columns("Description").SortMode = DataGridViewColumnSortMode.NotSortable
DataGridView1.Columns("Description").ReadOnly = True
End If
It didn't work. The glyph went away, but the data order stayed the same.
When I restart the program, sorted using the form button code and without clicking on the heading sort, I stopped the code and saw in the Immediate window that
DataGridView1.SortedColumn = Nothing
When I click on the Description column heading to sort the column data by description and stopped the code,
DataGridView1.SortedColumn was not nothing anymore.
I think I am looking for a way to reset DataGridView1.SortedColumn back to Nothing so that the grid will fill up with data from the dataset in the SQL sorted order. Can someone let me know if I am on the right track?
Continue reading...
strSQL = "SELECT * FROM AccessTable WHERE ID = 'P' OR ID = 'PD' OR ID = 'PN' ORDER BY [autoseq], [row_seq]"
Dim cn as New OleDbConnection(My.Settings.ConnString)
cn.Open()
Using daDB As New OleDbDataAdapter(strSQL, cn)
ds.Clear()
daDB.Fill(ds)
End Using
cn.Close()
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.Refresh
All works well. The data displays in the grid like so:
autoseq ID Description Code
60 P Description1 Code1 .....
61 PD Description2 Code2 .....
62 PN Description3 Code3 .....
63 P Description4 Code4 .....
...
When I click on the Description column heading, the grid sorts by Description, either ascending or descending. No problem. When I click on the form button that executes the above code, the DataGridView continues to display in description ascending or descending order. The data in the DataGridView does not display sorted by autoseq. I tried clearing DataGridView1 in a variety of ways but that didn't work.
I stopped the code at DataGridView1.Refresh. I used the Immediate window to see what was in some of the dataset records and they appeared to be in the desired sorted order. That is, ds.Tables(0).rows(0).item(0) was 60, ds.Tables(0).rows(1).item(0) was 61, ... but yet the Grid still displayed the data in Description asc or desc order with the appropriate glyph.
I then researched if I could somehow turn off SelectedColumn sort. I tried the following code and tried it before the cn.Open() and just before binding the datasource to the dataset:
If DataGridView1.SortedColumn IsNot Nothing Then
DataGridView1.Columns("Description").SortMode = DataGridViewColumnSortMode.NotSortable
DataGridView1.Columns("Description").ReadOnly = True
End If
It didn't work. The glyph went away, but the data order stayed the same.
When I restart the program, sorted using the form button code and without clicking on the heading sort, I stopped the code and saw in the Immediate window that
DataGridView1.SortedColumn = Nothing
When I click on the Description column heading to sort the column data by description and stopped the code,
DataGridView1.SortedColumn was not nothing anymore.
I think I am looking for a way to reset DataGridView1.SortedColumn back to Nothing so that the grid will fill up with data from the dataset in the SQL sorted order. Can someone let me know if I am on the right track?
Continue reading...