0

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()

Edited by nadiam: additional info

2
Contributors
1
Reply
14
Views
1 Year
Discussion Span
Last Post by tinstaafl
0

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

Edited by tinstaafl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.