How to convert DataRow to DataRowView

How to convert DataRow to DataRowView

Converting DataRow to DataRowView 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 DataRowView out of DataRow:

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)];

Share this post

Leave a Reply

Your email address will not be published.