CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
ADO.Net 2.0 Features – Copying DataView Content to DataTable

By Satheesh babu
Posted On Jun 26,2008
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category: ADO.Net
Print this article.

ADO.Net 2.0 Features – Copying DataView Content to DataTable

 

The Part 2 of this article will help us understanding ToTable() method of DataView object which is introduced newly in .Netframework 2.0. Before understanding this method we will have a short understanding on DataView object and its usages. Dataview is an object that gives us an option to filter, sort, and search the underlying DataTable. In reality, it does not store the actual data itself but gives us a logical view of the DataTable that satisfies the criteria given as filter.

This article will also help us to understand how it can be achieved with 1.x framework.

 

Copying Sorted DataView to DataTable in 1.x days

In 1.x days, there are no methods available in the framework to copy the content of a Dataview to DataTable after it is sorted or any row filter is applied. So, it can be done by enumerating sorted DataView’s row and copying it to a fresh DataTable object. The below code will help us in achieving that.

It creates a DataTable object with some sample data and it is sorted using DataView object. We have cloned the existing unsorted DataTable to a new DataTable(dtSorted) so that we can get the same structure, schema and constraints for the sorted DataTable. Then, we have copied the sorted DataView content to it by enumerating.

 

DataTable dt = new DataTable();

DataRow dr ;

DataColumn dc1 = new DataColumn();

dc1.ColumnName ="Name";

dt.Columns.Add(dc1);

dr= dt.NewRow();

dr[0] = "Satheesh";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[0] = "Vadivel";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[0] = "naveen";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[0] = "viru";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr[0] = "mithun";

dt.Rows.Add(dr);

 

System.Data.DataView dvSort = dt.DefaultView;

dvSort.Sort = "Name";

 

DataTable dtSorted=dvSort.Table.Clone();

int i = 0;

string [] ColNames = new string[dtSorted.Columns.Count];

foreach (DataColumn col in dtSorted.Columns)

{

       ColNames[i++] = col.ColumnName;                       

}

 

IEnumerator Ienum = dvSort.GetEnumerator();

while (Ienum.MoveNext())

{

       DataRowView drv = (DataRowView)Ienum.Current;

       DataRow drSorted = dtSorted.NewRow();

       try

       {

       foreach (string strName in ColNames)

              {

              drSorted[strName] = drv[strName];

              }

       }

       catch (Exception ex)

       {

       Response.Write(ex.Message);

       }

       dtSorted.Rows.Add(drSorted);

}

 

for(int j=0;j<dtSorted.Rows.Count;j++)

{

Response.Write(dtSorted.Rows[j]["Name"].ToString());

}

 


DataView.ToTable() method in ADO.Net 2.0

It creates and returns a new DataTable based on rows in an existing DataView. With this method ADO.Net 2.0 prevents the above difficulty by exposing 4 overloads.

 

Overloads

DataView.ToTable () 

Creates and returns a new DataTable based on rows in an existing DataView.

 

DataView.ToTable (String) 

Creates and returns a new DataTable based on rows in an existing DataView with a string table name as argument. 

 

DataView.ToTable (Boolean, String[]) 

Creates and returns a new DataTable based on rows in an existing DataView.  If the Boolean is true, the returned System.Data.DataTable contains rows that have distinct values for all its columns. The default value is false. The string array will take the column name list and will output the datable with the same columns and in the same order specified in the argument.

 

DataView.ToTable (String, Boolean, String[]) 

Creates and returns a new DataTable based on rows in an existing DataView. The string array will take the column name list and will output the datable with the same columns and in the same order specified in the argument.

 

Implementation

The above requirement is now can be done with the ToTable() method with very less effort.

 

        DataTable dt = new DataTable();

        DataRow dr ;

        DataColumn dc1 = new DataColumn();

        dc1.ColumnName ="Name";

        dt.Columns.Add(dc1);

 

        dr= dt.NewRow();

        dr[0] = "Satheesh";

        dt.Rows.Add(dr);

        dr = dt.NewRow();

        dr[0] = "Vadivel";

        dt.Rows.Add(dr);

        dr = dt.NewRow();

        dr[0] = "naveen";

        dt.Rows.Add(dr);

        dr = dt.NewRow();

        dr[0] = "viru";

        dt.Rows.Add(dr);

        dr = dt.NewRow();

        dr[0] = "mithun";

        dt.Rows.Add(dr);

 

        DataView dvSort = dt.DefaultView;

        dvSort.Sort = "Name";

 

        DataTable dtSorted = dvSort.ToTable();

        DataTableReader dtdr = dtSorted.CreateDataReader();

       

        while (dtdr.Read())

        {

            Response.Write(dtdr[0].ToString()+"<br>");

        }

 

        //Copy one dt to another

        DataTable finaldt = new DataTable();

        finaldt.Load(dtSorted.CreateDataReader());

        dtdr = finaldt.CreateDataReader();

        while (dtdr.Read())

        {

            Response.Write(dtdr[0].ToString() + "<br>");

        }

 

Reference

DataView.ToTable()

 

Conclusion

Thus, the new method that is packed with ADO.Net 2.0 will reduce the coding effort required in 1.x days. The 4 overloads present for the method will give us more flexibility to customize the output given by the method.

Happy Coding!!!

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments