How to sort data using DataGridView in VB.NET?

17,182

Solution 1

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 DataGridViewRows and set each row's visibility depending on the entered criteria:

For Each row As DataGridViewRow in dgv.Rows
    row.Visible = {some condition}
Next

Solution 2

'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

Solution 3

try following code:

this.dataGridView1.Sort (this.dataGridView1.Columns["Yourcolumnname"], ListSortDirection.Ascending);
Share:
17,182

Related videos on Youtube

user225269
Author by

user225269

Updated on June 04, 2022

Comments

  • user225269
    user225269 almost 2 years

    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.

    enter image description here

    • David-W-Fenton
      David-W-Fenton over 14 years
      Does this question have anything at all in it that's specific to Access? If not, then remove the tag. I don't see anything in it that relates to Access/Jet/ACE at all.
  • Ricardo Gomes
    Ricardo Gomes over 14 years
    you can definitely sort in a datagridview
  • M455y
    M455y almost 2 years
    this is C# not VB.NET