比如已知订单号,订单分号,在dbgrid中定位到相应的订单分号?我现在要子表里定位是用循环,实要是笨极了(学了两个月c#,看来还没有入门啊555): int danH = inq.DanHao;
int bianH = inq.BianHao; object[] criteria1 = new object[] {danH};
this.dsGoals1.DanInfo.DefaultView.Sort="DanHao";
int idx1 = this.dsGoals1.DanInfo.DefaultView.Find(criteria1);
this.BindingContext[this.dsGoals1,"DanInfo"].Position=idx1; int cnt = this.BindingContext[this.dsGoals1,"DanInfo.DanInfoAM"].Count;
while (this.BindingContext[this.dsGoals1,"DanInfo.DanInfoAM"].Position<cnt)
{
                    int bH = (GetRow(this.dGdAM.DataMember) as dsGoals.AMRow).BianHao;
if (bH == bianH)
break;
else
this.BindingContext[this.dsGoals1,"DanInfo.DanInfoAM"].Position++;
}
this.LoadAMData();

解决方案 »

  1.   

    整理一下:
    int danH = inq.DanHao;
    int bianH = inq.BianHao;
    //父表直接查找
    object[] criteria1 = new object[] {danH};
    this.dsGoals1.DanInfo.DefaultView.Sort="DanHao";
    int idx1 = this.dsGoals1.DanInfo.DefaultView.Find(criteria1);
    this.BindingContext[this.dsGoals1,"DanInfo"].Position=idx1;//用循环在子表中定位,很笨,也很慢。请大吓们请教下面的代码该怎么改啊
    int cnt = this.BindingContext[this.dsGoals1,"DanInfo.DanInfoAM"].Count;
    while (this.BindingContext[this.dsGoals1,"DanInfo.DanInfoAM"].Position<cnt)
    {
    int bH = (GetRow(this.dGdAM.DataMember) as dsGoals.AMRow).BianHao;
    if (bH == bianH)
    break;
    else
    this.BindingContext[this.dsGoals1,"DanInfo.DanInfoAM"].Position++;
    }this.LoadAMData();
      

  2.   

    你用DataRowCollection对象试试,我定位时也是循环,感觉速度还可以,而且结果集已排过序,可用折半查找法:DataSet ds = (DataSet)this.datagrid.DataSource;
    DataTable table = ds.Tables[0];
    DataRowCollection rows = table.Rows;
    for(int row = 0; row < rows.Count; row++)
    {
      //查找
      if(rows[row]["字段名"].toString.Equals("要比较的值"))
         ....
    }
      

  3.   

    谢谢  lldwolf(铁背苍狼) 。不过我的目的是要找到子表中的某行,并把它置为当前行。用你的办法似乎不行啊。