« Home | Deleting TFS Project »

Converting DataView into DataTable

In the middle of our programming there will be situations, where we have our dataview to be converted into datatable, may be for searching..

We can achieve this by using Enumerator.

Follow the below code to achieve this.

Assume objDataView is our DataView, which we are converting into DataTable named obNewDt.


DataTable obNewDt = objDataView.Table.Clone();
int idx = 0;
string[] strColNames = new string[obNewDt.Columns.Count];
foreach (DataColumn col in obNewDt.Columns)
{ strColNames[idx++] = col.ColumnName; }
IEnumerator viewEnumerator = objDataView.GetEnumerator();
while (viewEnumerator.MoveNext())
{
DataRowView drv = (DataRowView)viewEnumerator.Current;
DataRow dr = obNewDt.NewRow();
try
{
foreach (string strName in strColNames)
{
dr[strName] = drv[strName];
}
}
catch (Exception ex)
{
throw ex;
}
obNewDt.Rows.Add(dr);
}

Your DataTable obNewDt is ready for your further programming.

Happy Coding...

Labels: , , , , , , ,

Post a Comment

Translate

About me

Narayanan
Senior Software Engineer
Microsoft Certified Professional Developer
MCPD

Chat

Previous posts

Visitors