2

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 ?

2
  • Why are saying that strings cannot be sorted? Can you please post the code you have tried? Commented Nov 26, 2013 at 8:49
  • No , I meant, one of columns is numeric and I want to be sorted, but it considered as string for Ex. it must show : 1 2 3 4 5 6 7 8 9 10 11 12 but it shows : 1 10 11 12 ... my question is how can I convert this column to Integer , so it will work fine Commented Nov 26, 2013 at 9:00

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

Comments

1

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.