Search Asp.Net Here

How to convert a DataView to a DataTable or DataSet?






Convert a DataView to a DataTable or DataSet?


.Net framework has provided us a very useful class DataView that provide us the functionality to create desired view of data from a given DataTable or DataSet by specifying some row filtering or sorting expression. But many time we want to create a DataTable back from a DataView to perform some manipulation. In this example first I will create a DataView from a DataSet’s DataTable and than againg convert DataView into a DataTable

I have use the “NORTHWIND” database’s table “PRODUCTS” in my example


SqlDataAdapter adp = new SqlDataAdapter("select * from Products", "server=.;database=northwind;uid=sa");
DataSet ds = new DataSet();
adp.Fill(tb);
//Creating DataView
DataView dv = ds.Tables[0].DefaultView;
//Filtering Data in DataView
dv.RowFilter = "CategoryID=1";
//converting DataView back to DataTable using Table Property of DataView Class
DataTable tb = dv.Table;
// or usefToTable() method if you have done some sorting or filtering with DataView
DataTable tb1 = dv.ToTable();
GridView1.DataSource = tb;
GridView1.DataBind();


Comments :

1
Macrosoft said...
on 

Nice details about converting data view to data table.

Convert ASP to ASP.Net

Post a Comment