How would you be able to Capture the Value of a Cell when the User Selects a Row?

Dani AI

Generated

As 's suggestion shows, reading a value from the selected row and placing it in a TextBox is the right approach. For a more robust, future‑proof solution, prefer named columns over numeric indexes, handle empty/DBNull values, and make the grid's selection behavior predictable (FullRowSelect, single selection). The examples below assume the "second" column is known by name (safer than using index 1 if columns can be reordered).

A reliable event to use is SelectionChanged (or RowEnter when focus is required). This example uses the current row and a column name and avoids exceptions when nothing is selected or the cell contains DBNull:

Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
    Dim row = DataGridView1.CurrentRow
    If row Is Nothing Then
        TextBox1.Clear()
        Return
    End If

    Dim raw = row.Cells("SecondColumnName").Value
    TextBox1.Text = If(raw Is Nothing OrElse IsDBNull(raw), String.Empty, raw.ToString())
End Sub

If the grid is data‑bound (for example to a DataTable), accessing the bound item gives a typed, stable way to fetch the value:

Dim drv As DataRowView = TryCast(DataGridView1.CurrentRow?.DataBoundItem, DataRowView)
If drv IsNot Nothing Then
    TextBox1.Text = Convert.ToString(drv("SecondColumnName"))
End If

Troubleshooting notes: ensure SelectionMode = FullRowSelect and MultiSelect = False if a single row selection is required; SelectedRows may be empty when the grid uses cell selection; always guard against DBNull; and prefer RowEnter when updates must occur only when a row receives focus (SelectionChanged can fire more frequently). These adjustments complement the solutions that worked earlier in the thread from and while making the code safer for maintenance.

Recommended Answers

All 7 Replies

I guess that you are looking for a particular column in your datagridview.

DataGridView1.Item(0, DataGridView1.SelectedRows.Item(0).Index).Value

@Joshua Kidd : Not getting you.....

I have A Datagrid view. And I want the User to be able to select a Row, but when They Select that row is Gets the Value of the 2nd Cell in the 2nd Column and puts it in a TextBox.

textbox.text = DataGridView1.Item(1, DataGridView1.SelectedRows.Item(0).Index).Value

Make sure you've disabled multiple rows selection or that you've tested that the results meet your requirements.

or you can try this:

texbox1.Text =  DataGridView1.SelectedRows(0).Cells(1).Value.ToString()
textbox.text = DataGridView1.Item(1, DataGridView1.SelectedRows.Item(0).Index).Value

Make sure you've disabled multiple rows selection or that you've tested that the results meet your requirements.

Thanks very Much. This Worked ;)

Then please mark this thread as solved.
Thx.

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.