通过索引检索 DataView 中的行

发布于 2024-07-16 07:24:43 字数 187 浏览 5 评论 0原文

我有一个 DataView,它已按某种顺序排序。 如何使用索引检索值。

像这样的事情:

if(dv.rows[0]["name"]=="xxx")  
{  
  --- do something ---  
}  
else  
  --- something else ---  

I have a DataView, which has been sorted in some order. How can I retrieve the values using an index.

Something like this:

if(dv.rows[0]["name"]=="xxx")  
{  
  --- do something ---  
}  
else  
  --- something else ---  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜尕妞 2024-07-23 07:24:43

尝试以下代码

将排序后的 DataView 移动到 DataTable ,

DataTable dt = dv.ToTable(); 

然后使用

if (dt.Rows[0]["name"] == "xxx")
{
  [...]
}

它将起作用。

Try the following code

Move the sorted DataView to DataTable like

DataTable dt = dv.ToTable(); 

Then use

if (dt.Rows[0]["name"] == "xxx")
{
  [...]
}

It will work.

狼性发作 2024-07-23 07:24:43

你试过了吗:

DataRowView rowView = dv[index];

Did you try:

DataRowView rowView = dv[index];
不如归去 2024-07-23 07:24:43

您可以直接使用 DataView,而不是将整个内容转换回 Table:

要从 DataView 获取行,您可以使用 Item 属性,该属性返回一个DataRowView,然后您可以调用 Item 来获取单元格,所有这些都可以缩短为:

// Returns object, so needs to be cast to your required type:
if ((string)dv[0]["CellName"] == "ABCD")
{
  [...]
}

Rather than converting the whole thing back to a Table, you can work with the DataView directly:

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:

// Returns object, so needs to be cast to your required type:
if ((string)dv[0]["CellName"] == "ABCD")
{
  [...]
}
天邊彩虹 2024-07-23 07:24:43

我不太知道这是否是您正在寻找的答案:

if (dv.Rows[0].Cells["CellName"].Value == "ABCD")
{

}

I don't quite know if this is the answer you're looking for:

if (dv.Rows[0].Cells["CellName"].Value == "ABCD")
{

}
清引 2024-07-23 07:24:43

我认为,直接使用 DataRowView 比将 dv 转换回 DataTable(它可能来自 DataTable)更简单。
var currRow = dv[0]; currRow["your_col_name"]?.ToString();

The direct use of the DataRowView is simpler than converting the dv back to a DataTable (where it probably came from to begin with), I think.
var currRow = dv[0]; currRow["your_col_name"]?.ToString();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文