How Can Convert DataRow to DataRowView in c#

北战南征 提交于 2019-12-30 06:04:20

问题


Can or how to convert DataRow to DataRowView?

For example:

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

回答1:


Use

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



回答2:


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



回答3:


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

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




回答4:


Use. It also works if the DataGrid is ordered.

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


来源:https://stackoverflow.com/questions/15632036/how-can-convert-datarow-to-datarowview-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!