除了遍历去判断,有没有更好的方法?

解决方案 »

  1.   

    使用BindingSource试试,它相当于给DataTable加了层外套,有当前行指示比如Current
      

  2.   


    我说的遍历就是指循环判断DataTable指定列的数据是否与给定值相同,如果相同,取出该行的索引          for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i][1].ToString().Equals(str))
                        {
                            index= i;
                            break;
                        }
                    }如上面的代码,假设指定列1,index就是我要找的索引,我想知道除了这个方法有没有更好更快的办法?
      

  3.   

    用用这个试试
     
    C# 
    public DataRow[] Select (
    string filterExpression,
    string sort,
    DataViewRowState recordStates
    )
     
    参数
    filterExpression
    要用来筛选行的条件。 sort
    一个字符串,它指定列和排序方向。 recordStates
    DataViewRowState 值之一。 
      

  4.   

    调用示例:
        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);
      

  5.   

    好像DataTable这个类确实没有提供其它的方法。
      

  6.   

    dt.Select("列名='值'"),试试这样能否通过DataView找到DataTable中对应的行索引,没环境,没试过
      

  7.   


    dtStation.Select("Name='刘玲'")返回的DataRow[]怎么用啊??
      

  8.   


                    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,不知道是不是这样取不正取,没有得到相应的行索引。
      

  9.   

    DataRow[] rows = dtStation.Select("Name='刘玲'");
    if rows.Length > 0
    {
       foreach(DataRow row in rows)
       {
           //对Row进行操作
       }
    }()
      

  10.   

    刚才这样试过的结果是rows.Length=0,
    也就不能执行
    foreach(DataRow row in rows)
       {
           //对Row进行操作
       }
    说明这个方法不能查到索引
    正常按我之前的算法,这个名字是可以得到索引的