Problem getting value from the DataGridView component?

How do I get the value from an selected row with the DataGridView component to an Label component?

[105 byte] By [ekke] at [2007-12-23]
# 1

If you've got full row select something like will make label1 = selectedrow column 2 (Remember its 0 based collection)

Label1.Text = DGV1.SelectedRows.Item(0).Cells(1).Value

With fullrow select then use the selectedcells collection instead.

spotty at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

I mean it should work like this no matter if I choose row 2 or row 5 or something else, it should show

the value automatic in the Label component.

ekke at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

Whatever cell/row you choose, then the label will show the value. What code have you tried? What events are you triggering off?

you can simply try in the CellEnter:

Label1.Text=DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString

SJWhiteley at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4

This would work if you are using cell select and you are not allowing multiple cells to be selected (Multiselect property)

spotty at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5

Example

Add a datagridview with a two columns.

Use this code.

At some text in the cells and then start chnaging the selections.

Public Class Form1


Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
Label1.Text = Me.DataGridView1.SelectedRows.Item(0).Cells(1).Value
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Me.DataGridView1.MultiSelect = False


End Sub
End Class

spotty at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...