C# 通过索引检索 DataView 中的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/700869/
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
Retrieving rows in DataView by its index
提问by Huzefa
I have a DataView, which has been sorted in some order. How can I retrieve the values using an index.
我有一个 DataView,它已按某种顺序排序。如何使用索引检索值。
Something like this:
像这样的东西:
if(dv.rows[0]["name"]=="xxx")
{
--- do something ---
}
else
--- something else ---
采纳答案by seena
Try the following code
试试下面的代码
Move the sorted DataView to DataTable like
将排序后的 DataView 移动到 DataTable 中
DataTable dt = dv.ToTable();
Then use
然后使用
if (dt.Rows[0]["name"] == "xxx")
{
[...]
}
It will work.
它会起作用。
回答by timo2oo8
I don't quite know if this is the answer you're looking for:
我不太清楚这是否是您正在寻找的答案:
if (dv.Rows[0].Cells["CellName"].Value == "ABCD")
{
}
回答by Miha Markic
Did you try:
你试过了吗:
DataRowView rowView = dv[index];
回答by Zhaph - Ben Duguid
Rather than converting the whole thing back to a Table, you can work with the DataView directly:
您可以直接使用 DataView,而不是将整个内容转换回 Table:
To get a row from a DataView
, you use the Item
property, which returns a DataRowView
, and you can then call Item
to get at the cells, all of which can be shortened to:
要从 a 获取一行DataView
,您可以使用Item
返回 a的属性,DataRowView
然后您可以调用Item
获取单元格,所有这些都可以缩短为:
// Returns object, so needs to be cast to your required type:
if ((string)dv[0]["CellName"] == "ABCD")
{
[...]
}