How Can Convert DataRow to DataRowView in c#

半世苍凉 提交于 2019-11-30 17:51:22
BizApps

Use

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

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];
        }
DataRowView selecRow = dataTable.DefaultView.Cast<DataRowView>().FirstOrDefault(a => a.Row == desRow);

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

Use. It also works if the DataGrid is ordered.

DataRowView selecRow = dataTable.DefaultView.Cast<DataRowView>().Where(a => a.Row == desRow).FirstOrDefault();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!