pixelmeow
pixelmeow

Reputation: 654

DataGridView sort programmatically, case sensitive

I'm working on a project converting VB6 applications to VB.NET. It's on the first step, having gone through conversion through Visual Basic 2008. I'm replacing FlexGrids with DataGridViews and have come up against some problems I'm having a really hard time finding the answer to.

One problem is that the sorting on the FlexGrid had many options. The only options I see with DataGridView are:

System.ComponentModel.ListSortDirection.Ascending
System.ComponentModel.ListSortDirection.Descending

I need case sensitive sorting. I've looked everywhere and haven't found what I need. It was even hard finding the code above. Any help is very appreciated.

Edit: The grid is filled programmatically.

Upvotes: 1

Views: 1514

Answers (1)

OhBeWise
OhBeWise

Reputation: 5454

If you are manually filling the grid, then ListSortDirection.Ascending should be all you need.

For example, given the following list of Example items (in any order):

Dim examples = New List(Of Example)() From { _
    New Example() With { _
        Key .Bar = "Abcd", _
        Key .Foo = "3" _
    }, _
    New Example() With { _
        Key .Bar = "ABcd", _
        Key .Foo = "4" _
    }, _
    New Example() With { _
        Key .Bar = "aBcd", _
        Key .Foo = "2" _
    }, _
    New Example() With { _
        Key .Bar = "abcd", _
        Key .Foo = "1" _
    } _
}

With the following setup:

Dim col1 As New DataGridViewTextBoxColumn()
Dim col2 As New DataGridViewTextBoxColumn()

col1.Name = "Foo"
col2.Name = "Bar"

col1.SortMode = DataGridViewColumnSortMode.NotSortable
col2.SortMode = DataGridViewColumnSortMode.Programmatic

Me.dataGridView1.Columns.Add(col1)
Me.dataGridView1.Columns.Add(col2)

For Each example As Example In examples
    Me.dataGridView1.Rows.Add(example.Foo, example.Bar)
Next

Me.dataGridView1.Sort(col2, ListSortDirection.Ascending)

You should always see the results as:

DataGridView ascended sort for "Bar" column

Upvotes: 3

Related Questions