Saturday, May 1, 2010

How to convert Data Row to Data Row View

Converting Data Row to Data Row View is not possible, but we can go around this by using DefaultView property of the Data Row Table.

The following code example demonstrate how to get Data Row View out of Data Row:
DataTable dt = new DataTable();

dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));

for (int i = 0; i < 10; i++)
{
 DataRow tempDR = dt.NewRow();
 tempDR["ID"] = (i + 1);
 tempDR["Name"] = "Name " + (i + 1).ToString();
 dt.Rows.Add(tempDR);
}

DataRow dr = dt.Rows[1]; 
DataRowView drv = dr.Table.DefaultView[dt.Rows.IndexOf(dr)];

Source: https://www.nilebits.com/blog/2010/03/how-to-convert-datarow-to-datarowview/

No comments:

Post a Comment