在知道DataTable某一列数据的情况下,如何得到这个数据的行索引? 除了遍历去判断,有没有更好的方法? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用BindingSource试试,它相当于给DataTable加了层外套,有当前行指示比如Current 我说的遍历就是指循环判断DataTable指定列的数据是否与给定值相同,如果相同,取出该行的索引 for (int i = 0; i < dt.Rows.Count; i++) { if (dt.Rows[i][1].ToString().Equals(str)) { index= i; break; } }如上面的代码,假设指定列1,index就是我要找的索引,我想知道除了这个方法有没有更好更快的办法? 用用这个试试 C# public DataRow[] Select ( string filterExpression, string sort, DataViewRowState recordStates) 参数filterExpression要用来筛选行的条件。 sort一个字符串,它指定列和排序方向。 recordStatesDataViewRowState 值之一。 调用示例: string expression; string sortOrder; expression = "id > 5"; // Sort descending by column named CompanyName. sortOrder = "name DESC"; // Use the Select method to find all rows matching the filter. DataRow[] foundRows = customerTable.Select(expression, sortOrder, DataViewRowState.Added); 好像DataTable这个类确实没有提供其它的方法。 dt.Select("列名='值'"),试试这样能否通过DataView找到DataTable中对应的行索引,没环境,没试过 dtStation.Select("Name='刘玲'")返回的DataRow[]怎么用啊?? string str = "刘玲"; DataRow[] foundRows = dtStation.Select("Name='str'"); for (int i = 0; i < foundRows.Length; i++) { string strtest= foundRows[i][0].ToString(); }结果是foundRows.Length=0,不知道是不是这样取不正取,没有得到相应的行索引。 DataRow[] rows = dtStation.Select("Name='刘玲'");if rows.Length > 0{ foreach(DataRow row in rows) { //对Row进行操作 }}() 刚才这样试过的结果是rows.Length=0,也就不能执行foreach(DataRow row in rows) { //对Row进行操作 }说明这个方法不能查到索引正常按我之前的算法,这个名字是可以得到索引的 datagridview单元格存放图片 WCF 代理迷惑 怎么屏蔽axWebBrowser控件中网页的右键以及脚本错误? DataTable里数据的排列顺寻 WindowsService 调用 webService 这个存储过程怎么实现啊,在线等,谢谢!请各位大虾帮帮忙啊. reflector---史上最强大反汇编工具 女娃哈哈,Byte()数组反过来转成Integer怎么转呢? 上传文件的控制??顶者有分 关于SQL连接问题 错误“_Default”并不包含“bind”的定义 是什么问题 应该怎么改 泛型问题
我说的遍历就是指循环判断DataTable指定列的数据是否与给定值相同,如果相同,取出该行的索引 for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][1].ToString().Equals(str))
{
index= i;
break;
}
}如上面的代码,假设指定列1,index就是我要找的索引,我想知道除了这个方法有没有更好更快的办法?
C#
public DataRow[] Select (
string filterExpression,
string sort,
DataViewRowState recordStates
)
参数
filterExpression
要用来筛选行的条件。 sort
一个字符串,它指定列和排序方向。 recordStates
DataViewRowState 值之一。
string expression;
string sortOrder;
expression = "id > 5";
// Sort descending by column named CompanyName.
sortOrder = "name DESC";
// Use the Select method to find all rows matching the filter.
DataRow[] foundRows =
customerTable.Select(expression, sortOrder,
DataViewRowState.Added);
dtStation.Select("Name='刘玲'")返回的DataRow[]怎么用啊??
string str = "刘玲";
DataRow[] foundRows = dtStation.Select("Name='str'");
for (int i = 0; i < foundRows.Length; i++)
{
string strtest= foundRows[i][0].ToString();
}结果是foundRows.Length=0,不知道是不是这样取不正取,没有得到相应的行索引。
if rows.Length > 0
{
foreach(DataRow row in rows)
{
//对Row进行操作
}
}()
也就不能执行
foreach(DataRow row in rows)
{
//对Row进行操作
}
说明这个方法不能查到索引
正常按我之前的算法,这个名字是可以得到索引的