How do we Sort the data from a Dataset?

Showing Answers 1 - 2 of 2 Answers

Praveen Kumar

  • Sep 23rd, 2005
 

Sorting is similar to filtering, in that you specify a sort expression. A typical sort expression is simply the name of the column to sort by. For example, to sort by the OrderDate column, you specify the sort expression OrderDate. However, you can sort by the value of any expression, including calculated values. If you call a table's Select method, you pass the sort expression as a parameter. If you are using data views, you specify the sort expression as the value of the view's "Sort" property.EX:' Visual BasicDim filterExp As String = "Status = 'Active'"Dim sortExp As String = "City"Dim drarray() As DataRowDim i As Integerdrarray = dataSet1.Customers.Select(filterExp, sortExp, DataViewRowState.CurrentRows)For i = 0 To (drarray.Length - 1) listBox1.Items.Add( drarray(i)("City").ToString )Next// C#string filterExp = "Status = 'Active'";string sortExp = "City";DataRow[] drarray;drarray = dataSet1.Customers.Select(filterExp, sortExp, DataViewRowState.CurrentRows);for (int i=0; i < drarray.Length; i++){ listBox1.Items.Add(drarray[i]["City"].ToString());}

  Was this answer useful?  Yes

Urvil Shah

  • Jun 20th, 2007
 

There is no direct sorting provided by dataset or datatable.
Sorting and filtering facility is provided by dataview.
You can sort the data using DataView.


DataSet ds = new DataSet(); // assume that dataset has datatable name mytable.
DataTable dt = new DataTable();
dt = ds.mytable;


DataView dv = new DataView();
dv = dt.DefaultView;
dv.Sort = "Name"; //Name is the column name, using this you can apply sorting
rules.


DataGrid1.DataSource = dv;
DataFrid1.DataBind();

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions