I have a button for edit and delete. I include editbutton.enabled=false in page load when dataset if dataset is empty

SqlDataAdapter da = new SqlDataAdapter("select * from Book", con);
            da.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)
            {
                dataGridView1.DataSource = ds.Tables[0];
            }
            else
            {
                button2.Enabled = false;
            }

Now, when the user deletes a row and the datagridview is empty, i want the edit button to be disabled because clicking it without highlighted cell from datagridview will produce error. How can I do this?


This is my delete code

DialogResult dlg = MessageBox.Show("Are you sure?\nThere is no undo once the selected data is deleted", "Confirm Deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dlg == DialogResult.Yes)
            {
                SqlDataAdapter da = new SqlDataAdapter("DELETE FROM Book WHERE BookNumber = '" + dataGridView1.SelectedCells[2].Value + "'", con);
                da.Fill(ds);
                this.Display(); //
            }
            else
            {
                MessageBox.Show("Deletion cancelled");
            }

Maybe, adding an if statement inside an if statement will do, but is it possible?

I think it's working now by adding an if statement inside an if statement

if (dlg == DialogResult.Yes)
            {
                SqlDataAdapter da = new SqlDataAdapter("DELETE FROM Transactions WHERE Transaction_No = '" + dataGridView1.SelectedCells[0].Value + "'", con);
                da.Fill(ds);
                this.Display();
                if (ds.Tables[0].Rows.Count == 0)
                {
                    button2.Enabled = false;
                }
            }
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.