Ali
Ali

Reputation: 684

How to sort a column of a DataGridView in VB.net (String)

I have been trying to sort a column in DataGridView (vb.net) and as far as it is considered as string, can not be sorted properly. it shows something like this : 1 1 1 10 2 2 26 3 ...

any suggestion ?

Upvotes: 2

Views: 4407

Answers (2)

Morteza Bashardoost
Morteza Bashardoost

Reputation: 78

You must convert the datatype of the column to integer. because string is the default datatype for the columns. you can change the data type with following code:

datatable1.Columns.Add("col1", GetType(Integer))

Upvotes: 1

user2480047
user2480047

Reputation:

The default type of DataGridView Cells is String. But if you set a different type in the DataSource, the corresponding cells would behave accordingly. Sample code:

Dim dt As DataTable = New DataTable
dt.Columns.Add("col1", GetType(Integer))
dt.Rows.Add(1)
dt.Rows.Add(10)
dt.Rows.Add(2)

DataGridView1.DataSource = dt

If you sort the first column of DataGridView1, it would show a numerical behaviour (e.g., 1, 2, 10 or 10, 2, 1).

Upvotes: 1

Related Questions