I have 2 datagridview (later- dgView), textboxes and some buttons. Everything is bounded and connected with MS Access db.
The tables in db are related!!
1st dgView contains Name and Surname table.
2nd dgView contains Details.

Now, how can I disable a delete button when datagrid is empty. I'm using this code:

If SvjeIDTextBox.Text = "" Then
       Button2.Enabled = False
       Else
       Button2.Enabled = True
       End If

Let me explain:
The SvjeIDTextBox.Text is textbox with the number of selected row in datagridview (when user clicks into datagrid view, the number of current row /selected row/ is shown into that SvjeIDTextbox).

Why I want to enable/disable delete button:

When i delete everything in datagridview, then there is no data in dgView. When dgView is empty and I click "delete" button, it gives me an error.
I don't know how to fix this error, so I decided to disable button "delete" when nothing is in datagridview.

Funny thing, but, when I put the code into "delete" button and make an MouseHover event over the delete button or mousehover event for whole Form, code is working.
But, I'm not happy with it.

Please help.

I made modifications

If SvjeIDTextBox.Text = "" Then
            MsgBox("Can't delete no data in dgView")

        Else
            Dim ask1 As MsgBoxResult

            Nobugz.PatchMsgBox(New String() {"Yes", "Nou"})
            ask1 = MsgBox("Do you want to delete selected data", MsgBoxStyle.YesNo, "Warnung")
            If ask1 = MsgBoxResult.Yes Then
                Svjedodzbe2DataGridView.Rows.RemoveAt(Svjedodzbe2DataGridView.CurrentRow.Index)
                'Me.Svjedodzbe2BindingSource.RemoveCurrent()
            End If

        End If

Now it works.

Well, when I try the code:

If Svjedodzbe2DataGridView.Rows.Count = 0 Then
            Button2.Enabled = False

        Else
            Button2.Enabled = True

        End If

it doesn't work.
And I don't know where to put the code. I tried into dgGview, on form Load event...
Any ideas?

Every time u delete a record from the datagridview (database) make sure u load the datagridview again completely...and if suppose any data is returned then enable ur delete button else disable...

u can add the enabling and disabling of delete button in all ur control events wherever u r dealing with datagridview (database)

something like this:
this code is written in delete button click event

Dim i As Integer
          Dim ID As String
          i = DataGridView1.CurrentRow.Index
          ID = DataGridView1.Item(0, i).Value
          MsgBox("ID: " & ID.ToString)

          Dim myCommand1 As SqlCommand

          myCommand1 = New SqlCommand("DELETE FROM  Names Where ID = '" & ID & "'", Connection)
          myCommand1.ExecuteNonQuery()

          Try
               Dim myCommand As SqlCommand
               myCommand = New SqlCommand("SELECT * FROM Names", Connection)
               Dim dt As New DataTable
               dt.Load(myCommand.ExecuteReader)
               MsgBox("dt value: " & dt.Rows.Count.ToString)
               If dt.Rows.Count <= 0 Then
                    With DataGridView1
                         .DataSource = Nothing
                    End With
                    btnDelete.Enabled = False
               Else
                    With DataGridView1
                         .AutoGenerateColumns = True
                         .DataSource = dt
                    End With
                    btnDelete.Enabled = True
               End If
          Catch ex As Exception
               MsgBox(ex.Message)
          End Try

Edited 4 Years Ago by poojavb

You can make it just by placing the disable/enable code of the button in the datagridview cellclicked event. That's it!

This question has already been answered. Start a new discussion instead.