Hi, i have a datagridview : grd_ol and i added a button to it:

Dim btnColumn As New DataGridViewButtonColumn()
btnColumn.HeaderText = "Invoice"
btnColumn.Text = "View Invoice"
btnColumn.UseColumnTextForButtonValue = True
grd_ol.Columns.Add(btnColumn)

the table sort of looks like this:

| ORDER_ID | CUSTOMER_ID | STAFF_ID | ORDER_DATE | TOTAL(RM) | INVOICE |
| id1 | custid1 | staffid1 | 13/12/2016 | 12345 | 'View Invoice' |
| id2 | custid5 | staffid3 | 13/12/2016 | 178342 | 'View Invoice' |
| id3 | custid9 | staffid2 | 13/12/2016 | 1543 | 'View Invoice' |

how do i get the order_id of the row by buttonClick of 'view invoice'? is it on grd_ol_CellContentClick?

i thought something like this would work but theres like a blue line and says End of statement expected
grd_ol.Rows[DataGridView.SelectedRows[0].Index].Cells[0].Value.ToString()

In the CellContentClick handler, the DataGridViewCellEventArgs(e) contains a RowIndex property that you can then use to get the value you want from the DataGridView. Here's an example where OrderID is an integer and the invoice is shown in a different tabpage with a DataGridView:

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    If TypeOf DataGridView1.Columns(e.ColumnIndex) Is DataGridViewButtonColumn Then
        Dim orderId = Integer.Parse(DataGridView1.Rows(e.RowIndex).Cells("OrderID").Value.ToString)
        Dim sql = "Select * From Invoices Where OrderID = @orderId"
        'DataConnection is a global function that returns the connection object.'
        Dim cmd = New OleDbCommand(sql, DataConnection())
        cmd.Parameters.AddWithValue("@orderId", orderId)
        Dim da As New OleDbDataAdapter(cmd)
        ds.Tables("Invoice").Clear()
        da.Fill(ds.Tables("Invoice"))
        TabControl1.SelectedTab = TabPage2
    End If
End Sub
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.