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

Dani AI

Generated

is on the right track — use the grid’s cell-click handler to determine which row was clicked, then read the ORDER_ID from that row (not from the button cell). Quick checklist before reading values: verify e.RowIndex >= 0 (header clicks give -1), confirm the clicked column is your button column (check e.ColumnIndex or the column Name), and guard for nulls / conversion errors.

If your grid is bound to a DataTable/DataSet, it’s often cleaner to pull the underlying DataRow instead of reading cell text. Example (inside your CellClick/CellContentClick handler):

' get underlying DataRow when DataSource is a DataTable
If e.RowIndex < 0 Then Return

Dim drv = TryCast(grd_ol.Rows(e.RowIndex).DataBoundItem, DataRowView)
If drv IsNot Nothing Then
    Dim raw = drv("ORDER_ID")
    Dim orderId As Integer = 0
    If raw IsNot Nothing AndAlso Integer.TryParse(raw.ToString(), orderId) Then
        ' orderId is ready to use
    End If
End If

If you prefer using the selection collection (not recommended for button clicks unless you enforce FullRowSelect), the VB syntax differs from the C#-style brackets you posted. For example:

' correct VB syntax if using SelectedRows
If grd_ol.SelectedRows.Count > 0 Then
    Dim idText = Convert.ToString(grd_ol.Rows(grd_ol.SelectedRows(0).Index).Cells("ORDER_ID").Value)
End If

Troubleshooting notes: setting UseColumnTextForButtonValue = True makes the button cell show the same text for every row (it won’t contain the ID). Keep the ID in a hidden column or read it from the bound object/row. Prefer Integer.TryParse (or proper casting of your bound object) to avoid exceptions.

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.