Click here to Skip to main content
15,065,783 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
I am sorting the grid using DataView's "Sort" property. I am passing sort expression and the order in which the grid is to be sorted(ASC or DESC). But the problem is, since the column on which I am sorting the grid is listing the intergers, the sorting is happening as if it is a string. But I need integer sorting rather string sorting on that column.

The grid displays below as ascending order(string sorting)

But I need the columns to be displayed like this(Integer sorting)

What would be the solution
Updated 11-Sep-19 23:21pm

You need to handle the SortCommand event: MSDN[^] explains how - look under "Specifying Custom Sorting" for details
Raghavendra HG 17-Feb-11 6:52am
I have used custom sorting only. Sorting is happening with no problem. But string sorting is happening. I need integer sorting.
Hi Raghavendra HG,

Be sure your entry to the datagridview is numeric not string data
If not you must casting it before ordering.

I hope this helps you,
You have to first set property AllowSorting="True" and then generate event OnSorting="gridview_Sorting".
And then apply sorting on Datasource and Bind the data as per your sort order and column.

And for sorting dataview u can use Lambda Expression

DataView sorted = table.ApplySort((r, r2) =>
return ((int)r["IntValue"]).CompareTo(((int)r2["IntValue"]));

and then write commeon class for sorting ..
Cheers! :-D

Thank You!
Hi thank you for all your replies. I tried the below one.

dt.Columns.Add("SOWId", typeof(int));

You need to specify the type of the item template values before binding the data into data table by using 'typeof' keyword.

This solved the problem... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900