How to convert Datatable into Dataview?

 Posted by vishalneeraj-24503 on 12/8/2013 | Category: ADO.NET Interview questions | Views: 1961 | Points: 40

With the help of Datatable DefaultView property we can convert any Datatable into Dataview.

DataView is used for sorting and filtering purposes.

If we want to sort rows based on condition,then we will use DataView Sort method.

For ex:-

DataTable dt = new DataTable();

dt.Rows.Add("1", "Vishal");
dt.Rows.Add("2", "Rajesh");
dt.Rows.Add("3", "Prashant");
dt.Rows.Add("4", "Dharmesh");
dt.Rows.Add("5", "Nitin");

DataView dv = dt.DefaultView;
dv.Sort = "ID desc";

We can also Sort using datatable rows with Linq or DataTable'Select method.
But as Dataview is more efficient way to do this.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response