在特定的ID数据表中查找一行 [英] Find row in datatable with specific id

查看:122
本文介绍了在特定的ID数据表中查找一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表两列:

ID, Calls. 



我要如何找到什么叫值为,其中ID = 5

5可能是anynumber,它只是为例子。每一行都有一个唯一的ID。

5 could be anynumber, its just for example. Each row has a unique ID.

推荐答案

请字符串条件搜索,类似这样的:

Make a string criteria to search for, like this:

string searchExpression = "ID = 5"

然后使用。选择()数据表对象的方法,如下:

Then use the .Select() method of the DataTable object, like this:

DataRow[] foundRows = YourDataTable.Select(searchExpression);

现在,你可以通过成果循环,像这样的:

Now you can loop through the results, like this:

int numberOfCalls;
bool result;
foreach(DataRow dr in foundRows)
{
    // Get value of Calls here
    result = Int32.TryParse(dr["Calls"], out numberOfCalls);

    // Optionally, you can check the result of the attempted try parse here
    // and do something if you wish
    if(result)
    {
        // Try parse to 32-bit integer worked

    }
    else
    {
        // Try parse to 32-bit integer failed

    }
}

这篇关于在特定的ID数据表中查找一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆