How to sort data using DataGridView in VB.NET?
I do not have any idea on how to sort data using datagridview in VB.NET. How do I do this by making use开发者_运维问答 of Textbox to input my query, I'm currently using OLEDB. Here is a picture of what I am trying to do.
'declare this first:
imports system.componentmodel
'then put this code into a button or something
DGV.Sort(DGV.Columns(0), ListSortDirection.Ascending)
'DGv = datagridview
goodluck
Homework?
You cannot exactly sort data using a DataGridView
, but you can set the display order of data in a DataGridView
.
Set the SortedColumn
property of your DataGridView
object to the DataGridViewColumn
by which you want your data sorted. If you need a more complex sorting order, you might want to call the Sort
method with a custom IComparer
. Refer to the MSDN documentation for details.
Or do you actually want to filter your data by the two criteria for which there is an input line in your screenshot? In that case, I'm not exactly sure about the best solution. Probably you need to iterate through all DataGridViewRow
s and set each row's visibility depending on the entered criteria:
For Each row As DataGridViewRow in dgv.Rows
row.Visible = {some condition}
Next
Does the sort need to be dynamic? That is, does your customer need to be able to click on a header row and sort it?
If not, why not order the query coming back and then bind that object to the grid?
This would not be a good idea if the sort is dynamic because the cost of back and forth to the DB would not be worth it.
try following code:
this.dataGridView1.Sort (this.dataGridView1.Columns["Yourcolumnname"], ListSortDirection.Ascending);
精彩评论