Reputation: 2319
A remote service is returning a datatset which is not sorted. the table columns are 'Title','author','edition','price' we have been told that if price is 0 then the book is 'out of stock'.
I am implementing sorting functionality on it by converting it to DataView then sorting the dataview and then binding the gridview.
Every think is ok except when it is sorted by price, all 0 price comes on top. how can i do this? Please help. I need to place all 0 price at bottom, and sort row not having 0 as price?
EDIT:- Current Code is As follows
DataTable dt = dtu.Table(q);
if (dt.Rows.Count>0)
{
searchpanel.Visible = true;
norecfound.Visible = false;
}else{searchpanel.Visible = false;
norecfound.Visible = true;}
DataTable table = new DataTable();
table.Columns.Add("BookCode", typeof(string));
table.Columns.Add("TITLE", typeof(string));
table.Columns.Add("AUTHOR_NAME", typeof(string));
table.Columns.Add("EDITION", typeof(string));
table.Columns.Add("PRICE", typeof(int));
table.Columns.Add("ldistance", typeof(double));
//id,TITLE,AUTHOR_NAME,EDITION,PRICE
for (int i = 0; i < dt.Rows.Count; i++)
{
object[] ob = dt.Rows[i].ItemArray;
table.Rows.Add(ob[0].ToString(), ob[1].ToString(), ob[2].ToString(), ob[3].ToString(), ob[4].ToString(), getcorrespondingdist(ob));
}
DataView dataView = new DataView(table);
if (HiddenField1.Value.Length < 5)
{
if (Request.QueryString["sort"]!=null)
{
HiddenField1.Value = Request.QueryString["sort"];
}
else
{
HiddenField1.Value = " ldistance DESC";
}
}
dataView.Sort = HiddenField1.Value;
Upvotes: 0
Views: 1848
Reputation: 2145
I have created demo code for you, just use it with your code.
Suppose you have your data in datatable dt.
DataRow[] drnew = dt.Select("Price <> 0");
DataRow[] drzero = dt.Select("Price = 0");
DataTable dtfinal = new DataTable();
if (drnew != null && drnew.Count() > 0)
{
DataView dv = drnew.CopyToDataTable().DefaultView;
dv.Sort = "Price Desc";
dtfinal = dv.Table;
}
if (drzero != null && drzero.Count() > 0)
{
dtfinal.Merge(drzero.CopyToDataTable());
}
dtfinal will contain sorted data with 0 price at bottom.
Hope it helps you.
Thanks.
Upvotes: 3