bbesase
bbesase

Reputation: 861

Getting Info out of a DataGridView

I need to get the information that is in each cell located in the datagrid view of the rows that the user selects. I have the selecting rows down, I just need help with getting the information in the cell. Right now it just returns as DataGridVewRow { Index=7} when the data is really a string. Here is my function

Private Function getCoordinates()
    Dim dt As New DataTable
    Dim dt2 As New DataTable
    'Dim r As DataRow
    Dim n As Integer = 0
    Dim selectedItems As DataGridViewSelectedRowCollection = dgv.SelectedRows
    dt = dgv.DataSource
    dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    dgv.MultiSelect = True
    Dim i = dgv.CurrentRow.Index




    dt2.Columns.Add("Position")
    Try

        If selectedItems Is Nothing Then
            For n = 0 To dt.Rows.Count - 1
                dt2.Rows.Add(n)
                dt2.Rows(n)("Position") = dt.Rows.Item(n)("Mouse Position")
            Next

        Else

            For Each selectedItem As DataGridViewRow In selectedItems


                dt2.Rows.Add(selectedItem)
                dt2.Rows(selectedItem.Index)("Position") = dt.Rows.Item(selectedItem.Index)("Mouse Position")

            Next
        End If

    Catch ex As Exception
        MsgBox("Error", MsgBoxStyle.Exclamation, "Error!")
    End Try


    Return dt2


End Function

Upvotes: 0

Views: 155

Answers (1)

NoAlias
NoAlias

Reputation: 9193

You get the text of the cells via the DataGridViewRow's Cells property. The following example would get the text from the first cell in each selected row:

Dim strContents As String = String.Empty

For Each selectedItem As DataGridViewRow In selectedItems

     strContents += " First Cell's Text: " & selectedItem.Cells(0).Text

Next

Upvotes: 1

Related Questions