[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

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
  • 3
1 Solution
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));

Source: http://www.velocityreviews.com/forums/t84096-sorting-number-in-ascending-way-but-still-mixes-the-numbers.html
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

How to: Customize Sorting in the Windows Forms DataGridView

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


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now