Link to home
Start Free TrialLog in
Avatar of viola123
viola123

asked on

How to sort a numeric field in gridview?

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

ASKER CERTIFIED SOLUTION
Avatar of carlsiy
carlsiy
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of viola123
viola123

ASKER

hi
thanks a lot. it works well.