Solved

How to sort a numeric field in gridview?

Posted on 2008-10-19
2
1,397 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
Comment Utility
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
Comment Utility
hi
thanks a lot. it works well.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now