Harry Sarshogh
Harry Sarshogh

Reputation: 2197

How Can Convert DataRow to DataRowView in c#

Can or how to convert DataRow to DataRowView?

For example:

   DataTable dt=ds.Tables[0];
   DataRow dr= dt.NewRow();           
   DataRowView drv = ???? 

Upvotes: 13

Views: 31618

Answers (4)

omei
omei

Reputation: 51

DataRowView selecRow = dataTable.DefaultView.Cast<DataRowView>().FirstOrDefault(a => a.Row == desRow);

works, this is a litte shorter without "where" ;-)

Upvotes: 2

Use. It also works if the DataGrid is ordered.

DataRowView selecRow = dataTable.DefaultView.Cast<DataRowView>().Where(a => a.Row == desRow).FirstOrDefault();

Upvotes: 1

Subramani
Subramani

Reputation: 21

The above method does not work if the DataRow status is Detached. This code snippet could be used in such case:

        DataRow dRow = dataTable.NewRow();
        //...
        DataRowView dRowView = dRow.Table.DefaultView.AddNew();

        for (int i = 0; i < dRow.ItemArray.Length; i++)
        {
            dRowView.Row[i] = dRow[i];
        }

Upvotes: 2

BizApps
BizApps

Reputation: 6130

Use

DataTable dt=ds.Tables[0];
DataRow dr= dt.NewRow();         
DataRowView drv= dt.DefaultView[dt.Rows.IndexOf(dr)];

Upvotes: 29

Related Questions