Reset GridView Sorting on Button Click
Hi Guys,
When my application begins, it will run the SQL Fill command, which loads the data Order by Urgent. In context, rows that are considered "Urgent" will be on Top.
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.ProjectsTableAdapter.Fill(Me.EVC_ProjectsDataSet.Projects)
End Sub
Here is the SQL statement in Fill method
Code:
SELECT ID, PO, Site, Client, Description, Lead_Time, Job_Status, Production_Status, Last_Updated, Remark, Date_Of_Entry, Urgent
FROM Projects
ORDER BY Urgent
Now sometimes, the user might click on a column in the GridView, causing it to sort by some other fields.
Hence, I created a Refresh button, which will revert the GridView state to the initial where it should Order by Urgent again.
Code:
Private Sub refresh_btn_Click(sender As Object, e As EventArgs) Handles refresh_btn.Click
EVC_ProjectsDataSet.Clear()
Me.ProjectsTableAdapter.Fill(Me.EVC_ProjectsDataSet.Projects)
End Sub
By my understanding, the Refresh Button will call the Fill method, which should execute the initial SQL and Order by Urgent again. But upon testing, it does not work. The GridView is still sorted according to what the column was last click.
In Summary, when the Refresh Button is clicked, the GridView should revert to Sort by Urgent Column.
How do I do that?
Re: Reset GridView Sorting on Button Click
Firstly, can I just conform whether or not it actually is a GridView your using, or is it a DataGridView? Unless you're using a third-party control, one is an ASP.NET Web Forms server control and the other is a Windows Forms control, so rather different things. From your code, I'm guessing that it's actually a DataGridView. It's rather important that you use the correct names for things.
You obviously have a typed DataSet so I'm guessing that your DataTable is bound to a BindingSource and that is bound to a DataGridView. The data in the DataTable will be in the order you retrieved it. Always. That DataTable has an associated DataView and it's in that that the data can be sorted after retrieval. The BindingSource gets its data from that DataView and provides it to the DataGridView. When the user clicks a column header in the grid, it sends a message to the BindingSource and that sends it on to the DataView, which sorts the data. You can do whatever you like with the data, including flush it out and add new data, and the DataView will retain the instruction to sort by the clicked column and continue to do so.
If you want to remove that sorting then you need to instruct the DataView to do so. To do that, you would call the RemoveSort method of the BindingSource. In your case, I'd suggest doing that between the Clear and Fill calls, while there's no old or new data that is already sorted to slow the process down.
By the way, when you refresh, it would be more correct to clear the specific DataTable that you're populating rather than the DataSet. It may not matter here but there may be other cases where that would lose you other data that you wanted to keep, e.g. a list that is bound to a combo box column in the grid.
Re: Reset GridView Sorting on Button Click
Hi jmcilhinney,
Sorry for not being clear. It's DataGridView.
I added the RemoveSort as you recommend and it works, thanks.
Code:
Private Sub refresh_btn_Click(sender As Object, e As EventArgs) Handles refresh_btn.Click
EVC_ProjectsDataSet.Clear()
ProjectsBindingSource.RemoveSort()
Me.ProjectsTableAdapter.Fill(Me.EVC_ProjectsDataSet.Projects)
End Sub
Quote:
Originally Posted by
jmcilhinney
By the way, when you refresh, it would be more correct to clear the specific DataTable that you're populating rather than the DataSet. It may not matter here but there may be other cases where that would lose you other data that you wanted to keep, e.g. a list that is bound to a combo box column in the grid.
Sorry but I don't quite follow what you mean by DataTable and DataSet??? What is the difference?
Re: Reset GridView Sorting on Button Click
Quote:
Originally Posted by
Volkof
Sorry but I don't quite follow what you mean by DataTable and DataSet??? What is the difference?
The DataSet object is a collection of DataTable objects. So if you wipe out your DataSet, you have wiped out ALL the DataTables it contained. You could specify which DataTable in the DataSet to clear, which I'd guess would be something like myDT.Tables(0).Clear which would clear the first DataTable.
Re: Reset GridView Sorting on Button Click
Quote:
Originally Posted by
Volkof
Sorry but I don't quite follow what you mean by DataTable and DataSet??? What is the difference?
A DataSet is basically an in-memory representation of a database. Just as a database generally contains multiple tables, a DataSet can contain multiple DataTables. Just as a database table contains columns to describe the data and rows to store the data, so a DataTable contains DataColumns and DataRows. In this code:
Code:
Me.ProjectsTableAdapter.Fill(Me.EVC_ProjectsDataSet.Projects)
the DataSet is 'Me.EVC_ProjectsDataSet' and 'Me.EVC_ProjectsDataSet.Projects' is one of the DataTables it contains.