I am facing a problem,

I have pulled some records in DataGridView from database. Now I want that when I select some rows from DataGridView and click on command button, ID's of all selected rows should be displayed through message box.

By using followng code I am able to display ID but problem is, "When I select multiple rows and click on Button, msgbox shows only the ID of later selected row, kindly help me.

I'm using Visual Basic for Windows Form.

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        For i = 1 To DataGridViewIU68E.SelectedRows.Count
            Dim ID As Integer = DirectCast(DataGridViewIU68E.SelectedRows(0).Cells("EvaluationIDDataGridViewTextBoxColumn").Value, Integer)
            MsgBox(ID)
        Next
    End Sub

Maybe you can try some thing like this untested code:

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
    '
    '  Here declare a string to receive all the Ids returned
    '
    Dim txtMessage as String = ""
    For i = 1 To DataGridViewIU68E.SelectedRows.Count
        '
        '  If there is almost an id in the message, separate it from the next
        '
        If txtMessage.Length > 0 Then txtMessage &= ", "
        '
        '  Add the Id to the existing txtMessage
        '
        txtMessage &= DirectCast(DataGridViewIU68E.SelectedRows(0).Cells("EvaluationIDDataGridViewTextBoxColumn").Value, Integer).ToString()
    Next
    '
    '  And show the result
    '
    MsgBox(txtMessage)
End Sub

Hope this helps

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.