Use DataViev object with each sort - ADO.NET

Q.  If you are using the DataSet and you have to display the data in sorted order what will you do?
- Published on 19 Oct 15

a. Use Sort method of DataTable
b. Use Sort method of DataSet
c. Use DataViev object with each sort
d. Use datapaging and sort the data.

ANSWER: Use DataViev object with each sort
 
DataView.Sortproperty allow you to sort data. Using a DataView, you can show the data in a table with different sort orders.

Example.
In this example our table name is StudentMaster.
public partial class Default5 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("provide connection string");
SqlDataAdapter da;
protected void Page_Load(object sender, EventArgs e)
{
string query = "select * from StudentMaster";
da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
dv.Sort = "studName";
DataTabledt = dv.Table;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)