Reading the current selected row number in a DataGridView


  • Hi,

    Can anyone tell me how to read the current selected row/column number of a DaraGridView (in C#). I thought this would be simple like some other controls like listboxes, but it has given me a bit of grief!

    I have disabled the multiple selection property on my DataGridView, so only one row can ever be the selected row.



    Wednesday, November 15, 2006 10:03 AM


All replies

  • Try this: [ assuming "view" is a DataGridView ]

    selectedRowNumber = view.SelectedRows.Count > 0 ? view.SelectedRows[0] : -1;

    If you know that only one row can be selected, the 0 index of the selectedrows should return it. If there are no selected rows, it returns -1.

    Wednesday, November 15, 2006 9:14 PM
  • hi,

    Were you able to solve the problem?


    Wednesday, November 15, 2006 9:31 PM
  • DataGridView.CurrentCellAddress.X should give you what you want.


    Wednesday, November 15, 2006 10:27 PM
  • well, if you have selected selectionmode=FullRowSelect in the properties of the datagrid view and multiselect = false maybe this code work for you, and give you the oportunity of make the changes when the user move in the datagridview control.

        Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
            ID_Sel.Text = DataGridView1.Rows(e.RowIndex).Cells("ID").Value
        End Sub
    Wednesday, November 29, 2006 12:44 AM
  • That would, I believe, give you the current column. DataGridView.CurrentCellAddress.Y should give you the current row.

    Thursday, May 17, 2012 5:34 PM