DataGrid Sorting

perform integer column sorting on datagrid.
I want to sort datagrid on the basis of colun header click.
My datagrid is bind to datasource which has one column that is varchar type. But it displays integer values in the datagrid.
The problem is it is not sorting integer value and treat it like string.
for example - 1 , 2,11,3,55
after sorting 1,11,2,3,55 instead of 1,2,3,11,55
Who is Participating?
DhaestConnect With a Mentor Commented:
How to: Customize Sorting in the Windows Forms DataGridView

DataGrid Customization Part-II: Custom Sorting and DataGrid Column Hiding

As for the sorting problem you mentioned, the problem is likely due to the DataTable's Column type. How do you add the DataColumn into DataTable? The DataColumn has several constructors which
mapping to the DataTable.Columns.Add methods such as
Add(string, Type)

#DataColumn Constructor

and if you add the column as DataTable.Columns.Add("column name"); then, the column is added as a stirng type column and when being sorting, the column's data will be sort as ASCII index sorting.
So if you want to have a int or float type column being sort by number, you need to add this column and specify the column type explicitly, such as DataTable.Columns.Add("int column", typeof(int));
DataTable.Columns.Add("float column",typeof(float));

replyanshuAuthor Commented:
The column type is decleared in database and we can not change the type of coloumn from string to int is there any other way around?
Did you take a look at the  the sort_command sub is below:

Dim y As Integer
Dim x As Integer = 0
Dim dv3 As New DataView
dv3 = Session("ds") 'the dataset is cached
y = dv3.Count
While x < y
'convert each item in the Dataview back to type Decimal
dv3.Item(x).Row.Item("CustBenefit") =
x += 1
End While
'sort all the decimals
dv3.Sort = e.SortExpression
'set the datasource and re-bind it
dgResults.DataSource = dv3

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.