Hi! Can someone help me?
I have a datagridview and I declared this code:

i = dgStudentsHandled.CurrentRow.Index
txtStudentNumber.Text = dgStudentsHandled.Item(0, i).Value

.

I can get the value of the first cell of the first row of the first column. But I have to get the value of the first cell of the the first column of the any row index I click. Thank you very much and God Bless. :)

Dani AI

Generated

A concise follow-up to 's question and 's suggestion: placing the retrieval code in the right event (CellClick or RowHeaderMouseClick) fixes the immediate issue, but a safer pattern is to read the event's row index (e.RowIndex / e.RowIndex) and read the cell or the bound item directly. This avoids relying on CurrentRow (which can be Nothing or change after the click) and prevents errors when clicking column headers. The examples below show robust, minimal handlers and a data‑bound approach.

Private Sub dgStudentsHandled_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgStudentsHandled.RowHeaderMouseClick
    If e.RowIndex < 0 Then Exit Sub
    Dim val = dgStudentsHandled.Rows(e.RowIndex).Cells(0).Value
    If val IsNot Nothing Then
        txtStudentNumber.Text = val.ToString()
    Else
        txtStudentNumber.Clear()
    End If
End Sub

When the grid is bound to a DataTable/BindingSource it is more reliable to read the DataBoundItem by column name instead of using a numeric index (column order can change):

Private Sub dgStudentsHandled_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgStudentsHandled.RowHeaderMouseClick
    If e.RowIndex < 0 Then Exit Sub
    Dim drv As DataRowView = TryCast(dgStudentsHandled.Rows(e.RowIndex).DataBoundItem, DataRowView)
    If drv IsNot Nothing Then
        txtStudentNumber.Text = drv("StudentNumber").ToString()
    Else
        ' fallback to cell read if not data-bound
        Dim v = dgStudentsHandled.Rows(e.RowIndex).Cells("StudentNumber").Value
        txtStudentNumber.Text = If(v Is Nothing, String.Empty, v.ToString())
    End If
End Sub

Troubleshooting and best practices:

  • Always check e.RowIndex >= 0 to skip header clicks.
  • Guard against Nothing (null) before calling ToString().
  • Prefer column names (or DataBoundItem) for maintainability rather than hard-coded column indexes.
  • For keyboard selection or programmatic changes, handle SelectionChanged or BindingSource.CurrentChanged instead of only mouse events.

Recommended Answers

All 3 Replies

>>value of the first cell of the the first column of the any row index you click.
just put your code on the right event.

Private Sub dgStudentsHandled_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgStudentsHandled.CellClick
   Dim i As Integer
   i = dgStudentsHandled.CurrentRow.Index
   txtStudentNumber.Text = dgStudentsHandled.Item(0, i).Value
End Sub

Thank you very much and God Bless. We used the same concept. :)

you're welcome..
if this thread already done then please mark as solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.