c#中如何将DataRow转换为DataRowView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15632036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Can Convert DataRow to DataRowView in c#
提问by Harry Sarshogh
Can or how to convert DataRow to DataRowView?
可以或如何将DataRow 转换为DataRowView?
For example:
例如:
DataTable dt=ds.Tables[0];
DataRow dr= dt.NewRow();
DataRowView drv = ????
采纳答案by BizApps
Use
用
DataTable dt=ds.Tables[0];
DataRow dr= dt.NewRow();
DataRowView drv= dt.DefaultView[dt.Rows.IndexOf(dr)];
回答by Subramani
The above method does not work if the DataRow status is Detached. This code snippet could be used in such case:
如果 DataRow 状态为 Detached,则上述方法不起作用。在这种情况下可以使用此代码片段:
DataRow dRow = dataTable.NewRow();
//...
DataRowView dRowView = dRow.Table.DefaultView.AddNew();
for (int i = 0; i < dRow.ItemArray.Length; i++)
{
dRowView.Row[i] = dRow[i];
}
回答by Nayara Ferreira de Jesus
Use. It also works if the DataGrid is ordered.
用。如果订购了 DataGrid,它也可以工作。
DataRowView selecRow = dataTable.DefaultView.Cast<DataRowView>().Where(a => a.Row == desRow).FirstOrDefault();
回答by omei
DataRowView selecRow = dataTable.DefaultView.Cast<DataRowView>().FirstOrDefault(a => a.Row == desRow);
works, this is a litte shorter without "where" ;-)
有效,这是一个没有“where”的短一点;-)