Marco Giglio
Marco Giglio

Reputation: 19

VB.NET and DataGridView: how read a specific Cell value

I have a DataGridView of 3 columns and 3 rows (it's just an example). I would like to read only the value of a specific cell (in this example 2x2). How do you do this? I try this code but visual studio write me there is an error:

"An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information:

Index non compreso nell'intervallo. Richiesto valore non negativo e minore della dimensione della raccolta."

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    'read array and send to DataGridView

    Dim cella(3, 3) As String

    cella(0, 0) = "Papa"
    cella(0, 1) = "Mamma"
    cella(0, 2) = "Bimbo1"
    cella(0, 3) = "Bimbo2"

    cella(1, 0) = "Bianco"
    cella(1, 1) = "Rosso"
    cella(1, 2) = "Nero"
    cella(1, 3) = "Blu"

    cella(2, 0) = "Firenze"
    cella(2, 1) = "Pisa"
    cella(2, 2) = "Livorno"
    cella(2, 3) = "Empoli"

    cella(3, 0) = "Gatto"
    cella(3, 1) = "Cane"
    cella(3, 2) = "Panda"
    cella(3, 3) = "Macaco"

    For x As Integer = 0 To 3
        DataGridView1.Columns.Add("newColumnName", "Testo")
    Next

    For y As Integer = 0 To cella.GetUpperBound(0)
        DataGridView1.Rows.Add(cella(0, y), cella(1, y), cella(2, y), cella(3, y))
    Next

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
    'Create MsgBox with the value of the specified cell 
    MsgBox(DataGridView1.SelectedRows.Item(2).Cells(2).Value.ToString())

End Sub

Upvotes: -1

Views: 149

Answers (2)

Heinz Z.
Heinz Z.

Reputation: 1574

The Exception occur because you have no SelectedRows. If you want only a fix cell try Row insteed of SelectedRows:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click    
  'Create MsgBox with the value of the specified cell 
  MsgBox(DataGridView1.Rows.Item(2).Cells(2).Value.ToString())
End Sub

Upvotes: 0

djv
djv

Reputation: 15774

Don't store the data in the UI. UI is for display and interaction, not for storing state.

Create a class to represent your data

Private Class Datum
    Public Sub New(name As String, color As String, city As String, animal As String)
        Me.Name = name
        Me.Color = color
        Me.City = city
        Me.Animal = animal
    End Sub
    Public Property Name As String
    Public Property Color As String
    Public Property City As String
    Public Property Animal As String
End Class

then populate the list with instances of the class and bind the data to the DataGridView

Private data As List(Of Datum)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    data = New List(Of Datum) From
        {
            New Datum("Papa", "Bianco", "Firenze", "Gatto"),
            New Datum("Mamma", "Rosso", "Pisa", "Cane"),
            New Datum("Bimbo1", "Nero", "Livorno", "Panda"),
            New Datum("Bimbo2", "Blu", "Empoli", "Macaco")
        }
    DataGridView1.DataSource = data
End Sub

and finally find the item you are looking for using some logic

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim city = data?.Single(Function(d) d.Name = "Bimbo1").City
    MessageBox.Show(city)
End Sub

enter image description here

Now your data is off the UI, and you can access it using logical names, pass the data around, query it, etc.

Upvotes: 1

Related Questions