您可以遍历DataTable 的每一行并检查其值。
我非常喜欢在使用 IEnumerables 时使用 foreach 循环。使查看或处理每一行变得非常简单和干净
DataTable dtPs = // ... initialize your DataTable
foreach (DataRow dr in dtPs.Rows)
{
if (dr["item_manuf_id"].ToString() == "some value")
{
// do your deed
}
}
或者,您可以将PrimaryKey 用于您的DataTable。这有多种帮助,但您通常需要先定义一个,然后才能使用它。
在http://msdn.microsoft.com/en-us/library/z24kefs8(v=vs.80).aspx使用 if 的示例
DataTable workTable = new DataTable("Customers");
// set constraints on the primary key
DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));
workCol.AllowDBNull = false;
workCol.Unique = true;
workTable.Columns.Add("CustLName", typeof(String));
workTable.Columns.Add("CustFName", typeof(String));
workTable.Columns.Add("Purchases", typeof(Double));
// set primary key
workTable.PrimaryKey = new DataColumn[] { workTable.Columns["CustID"] };
定义主键并填充数据后,您可以使用 Find(...) 方法获取与主键匹配的行。
看看http://msdn.microsoft.com/en-us/library/y06xa2h1(v=vs.80).aspx
DataRow drFound = dtPs.Rows.Find("some value");
if (drFound["item_manuf_id"].ToString() == "some value")
{
// do your deed
}
最后,您可以使用 Select() 方法在DataTable 中查找数据,该地址也位于http://msdn.microsoft.com/en-us/library/y06xa2h1(v=vs.80).aspx。
String sExpression = "item_manuf_id == 'some value'";
DataRow[] drFound;
drFound = dtPs.Select(sExpression);
foreach (DataRow dr in drFound)
{
// do you deed. Each record here was already found to match your criteria
}