?
Solved

How to sort a numeric field in gridview?

Posted on 2008-10-19
2
Medium Priority
?
1,405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
carlsiy earned 2000 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 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