Solved

How to sort a numeric field in gridview?

Posted on 2008-10-19
2
1,398 Views
Last Modified: 2012-05-05
Hi all,
i need to make a numeric field sortable in my gridview.
i did assign a numeric field to my gridview, but when i sort this column, it seems treat it like string:


DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("No");
 ds.Tables.Add(dt);
 
for (int i = 0; i < dsReview.Tables[0].Rows.Count; i++)
{
DataRow dr = ds.Tables[0].NewRow();
dr["NO"] = i+1; //bind this numeric field and need to sort by this field, but when i found the gridview treat it as a string, how to make it work????
ds.Tables[0].Rows.Add(dr);
}
 
DataView dv = new DataView(ds.Tables[0]);
dv.Sort = ViewState["SortingField"].ToString() + " " + ((SortDirection)ViewState["SortingDir"] == SortDirection.Ascending ? "ASC" : "DESC");
gvReview.DataSource = dv;
gvReview.DataBind();

Open in new window

0
Comment
Question by:viola123
2 Comments
 
LVL 6

Accepted Solution

by:
carlsiy earned 500 total points
ID: 22754505
By default, all textboxcolumns in .net are represented as string. however you could passing the data to the datatable as a datatype of Numeric so that it can be sorted accordingly.
see sample...

DataTable dt_Request = new DataTable();

DataColumn column;
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "request_no";
column.ReadOnly = true;
column.Unique = false;
dt_Request.Columns.Add(column);
 
you can then fill the data to the dt_Request datatable then bind it to a grid. you can now apply the proper sorting procedures.
0
 
LVL 6

Author Comment

by:viola123
ID: 22754765
hi
thanks a lot. it works well.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question