1

I have created a command button for each row on a datagridview.


datagridview row buttons


The code is working fine.

    Private Sub dgv_employees_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_employees.CellClick
    'If dgv_employees.Columns(e.ColumnIndex).HeaderText = "Edit" Then
    If e.ColumnIndex = 16 Then
        'dgv_employees.Columns(16).SortMode = DataGridViewColumnSortMode.NotSortable
        Dim constr As String = "Data Source=CARSON-PC;Initial Catalog=payroll;Integrated Security=True"
        Dim row As DataGridViewRow = dgv_employees.Rows(e.RowIndex)
        Dim ds As New DataSet()

        If MessageBox.Show(String.Format("Do you want to delete ID: {0}", row.Cells("empNum").Value), "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("DELETE FROM [dbo].[emp_personal] WHERE empNum = @empNum", con)
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.AddWithValue("@empNum", row.Cells("empNum").Value)
                    con.Open()
                    cmd.ExecuteNonQuery()
                    con.Close()
                End Using
            End Using

            Me.BindGrid()
        End If

    End If

End Sub

However, whenever I click the column header, it gives me an error.

How can I fix this?


error

Io-oI
  • 2,514
  • 3
  • 22
  • 29
LEE CARSON
  • 21
  • 6
  • If the user clicks on a column header, then `e.RowIndex` will be -1. Hence your error. It would be wise to check this value before you try to grab `dgv_employees.Rows(e.RowIndex)`. A check would look something like… `If (e.RowIndex >= 0) {…}` – JohnG Dec 17 '19 at 03:53
  • Please show me how..tried a lot of possible solutions for days but to no avail. The error is on this line "Dim row As DataGridViewRow = dgv_employees.Rows(e.RowIndex)" – LEE CARSON Dec 17 '19 at 04:03
  • I tried this code in replacement "If e.RowIndex = -1 AndAlso (e.ColumnIndex = 14 Or e.ColumnIndex = 15) Then" still, it's not working. – LEE CARSON Dec 17 '19 at 04:06
  • I also tried this one "If e.RowIndex >= 0 AndAlso e.ColumnIndex = 15 Then"...It's not working either... – LEE CARSON Dec 17 '19 at 04:08

2 Answers2

0

I think the issue is arising because of sorting attempt, I'd use following code to disable sorting on those DataGridViewButtonColumns.

 DataGridView.Columns.Item(<columnIndex or "Name">).SortMode = DataGridViewColumnSortMode.Programmatic

You can also avoid to use the negative index, fixing just the consequences:

Dim row as DataGridViewRow
If e.RowIndex >=0 Then
    row = dgv_employees.Rows(e.RowIndex)
End If    

Note, that there might be some context I don't know and thus further modifications requried.

Possibly, you could also use Overides to disable the click event in the header itself: disable-sorting-when-clicking-datagridview-column-header

Oak_3260548
  • 1,882
  • 3
  • 23
  • 41
0

Posting the code I've came up so others may have reference.

I deleted this line Dim row As DataGridViewRow = dgv_employees.Rows(e.RowIndex)

This one works now for me:

        If e.RowIndex >= 0 AndAlso e.ColumnIndex = 16 Then
        If MessageBox.Show(String.Format("Do you want to delete ID: {0}", dgv_employees.Rows(e.RowIndex).Cells("empNum").Value), "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("DELETE FROM [dbo].[emp_personal] WHERE empNum = @empNum", con)
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.AddWithValue("@empNum", dgv_employees.Rows(e.RowIndex).Cells("empNum").Value)
                    con.Open()
                    cmd.ExecuteNonQuery()
                    con.Close()
                End Using
            End Using

            Me.BindGrid()
        End If
    End If
halfer
  • 19,824
  • 17
  • 99
  • 186
LEE CARSON
  • 21
  • 6