private void dataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
{
DataRowView drv = (DataRowView)dataGridView.BindingContext[dataGridView.DataSource, dataGridView.DataMember].Current;
DataRow dr = drv.Row;
}
经检验发现这里获取的当前行是点击的前一行,微软的文档也是如此解释,通过e参数可以在datagridview上得到值,但是现在想在DataGridView的数据源上获取当前行的值,大家有什么好的办法没有呢?
微软的一些细节很郁闷.

解决方案 »

  1.   

    这里不应该理解为是点击的前一行吧,因为哪个行索引是从0开始算的,视觉上给人是点击的前一行.通过e参数可以获取啊.
    DataGridViewRow row = (sender as DataGridView).Rows[e.RowIndex];
      

  2.   

    MARK.
      

  3.   

    数据源上获取当前行应该用 CurrencyManager 类,使用方法如下(例子是帮助文档里的):
    Private myCurrencyManager As CurrencyManager Private Sub BindControl(myTable As DataTable)
        ' Bind a TextBox control to a DataTable column in a DataSet.
        TextBox1.DataBindings.Add("Text", myTable, "CompanyName")
        ' Specify the CurrencyManager for the DataTable.
        myCurrencyManager = CType(me.BindingContext(myTable), CurrencyManager)
        ' Set the initial Position of the control.
        myCurrencyManager.Position = 0
     End Sub Private Sub MoveNext(myCurrencyManager As CurrencyManager)
        If myCurrencyManager.Position = myCurrencyManager.Count - 1 Then 
           MessageBox.Show("You're at end of the records")
        Else
           myCurrencyManager.Position += 1
        End If
     End Sub Private Sub MoveFirst(myCurrencyManager As CurrencyManager)
        myCurrencyManager.Position = 0
     End Sub Private Sub MovePrevious(myCurrencyManager As CurrencyManager)
        If myCurrencyManager.Position = 0 Then
           MessageBox.Show("You're at the beginning of the records.")
        Else
           myCurrencyManager.Position -= 1
        End if
     End Sub Private Sub MoveLast(myCurrencyManager As CurrencyManager)
        myCurrencyManager.Position = myCurrencyManager.Count - 1
     End Sub
      

  4.   

    每当数据源发生变化时myCurrencyManager.Position都是会变的
      

  5.   


    是要在DataGridView的DataSource上获取需要的字段值,而不是在DataGrid上面获取值.
      

  6.   

    object o = ((DataTable)dataGridView1.DataSource).Rows[rowIndex]["columnName"];
    object o = ((DataSet)dataGridView1.DataSource).Tables[0].Rows[rowIndex]["columnName"];