Solved

How to sort a numeric field in gridview?

Posted on 2008-10-19
2
1,399 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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