通过主键进行比较,我做软件时的代码如下(this.m_KeyCol表示主键所在的列,整型):
/// <summary>
/// 根据DataGrid的内容,返回当前DataSet中的行的索引
/// </summary>
/// <returns>成功返回索引,否则返回-1</returns>
protected int CurRowIndex
{
get
{
int result=-1;
if(this.m_Table.Rows.Count>0)
{
string keyvalue=this.dataGridInfo[dataGridInfo.CurrentRowIndex,this.m_KeyCol].ToString();
for(int i=0;i<this.m_Table.Rows.Count;i++)
{
if(this.m_Table.Rows[i][m_KeyCol].ToString()==keyvalue)
{
//找到
result=i;
break;
}
}//end for
}
return result;
}
set
{
int newPos=(int)value;
if(newPos>=0 && newPos<m_Table.Rows.Count && dataGridInfo.CurrentRowIndex!=newPos)
this.dataGridInfo.CurrentRowIndex=newPos;
}
}

解决方案 »

  1.   

    ((DataSet)this.dgMain.DataSource).Tables[xxx].Rows[xxx]
      

  2.   

    另外,将dataGridInfo.CurrentRowIndex换成具体其他数字就可以得到其他行了,得到行号后用DataRow row=m_Table.rows[nIndex];之类就可以了应用了
      

  3.   

    上面的老兄,如果DataGrid重新排序,就不行了。如何实现?
      

  4.   

    通过DataView去获取,如果绑定的是DataSet 就((DataSet)this.dgMain.DataSource).Tables[xxx].DefaultView[Index].Row
      

  5.   

    用dataGrid1[row,col],得到某个值,再根据这个值找DataRow对应的行