0

In my windows form application, I have a categories form that contain different categories with their ID's, names and description. The form also contain several buttons for different operations including adding a category, editing and deleting a category.

In some cases, if the categories DataGridView needs to be empty, how may I inform a user that the gridview is already empty and that no further deletion can be possible. I want to notify him via messagebox that there is no more rows remaining to be deleted. Following is the code that I have used on delete button.

private void btnDelete_Click(object sender, EventArgs e)
    {
        if (dgvCategories.Rows.Count == 0)
        {
            DataAccess.WarnUser("No categories to delete.");
            txtCategoryName.Focus();
            return;
        }

        string categoryName = dgvCategories.CurrentRow.Cells[1].Value.ToString();
        if (DataAccess.AskUser("Are you sure want to delete this permanently?", "Confirm Deletion") == true)
        {
            string categoryId = dgvCategories.CurrentRow.Cells[0].Value.ToString();
            Categories.Delete(categoryId);
            FillGrid();
        }
    }

But I am getting "Object reference not set to an instance of an object." error on this line. What am I doing wrong here?

string categoryName = dgvCategories.CurrentRow.Cells[1].Value.ToString();
Noor
  • 185
  • 2
  • 18
  • Isn't this what your code is already doing (assuming that `DataAccess.WarnUser()` shows a message box)? – Nico Schertler Jul 12 '18 at 20:14
  • @Nico off-coarse this is actually a message box that will show me a message with warning icon. But still I am getting {"Object reference not set to an instance of an object."} error when I still click on delete button when the grid is already empty. – Noor Jul 12 '18 at 20:20
  • Then you should add this detail to your question. Also, where are you getting this exception? Please check [this question](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) for help with this error. Also, I am not sure if the `CurrentRow` could be `null` without the grid being empty. It might be better to check for `CurrentRow == null`. – Nico Schertler Jul 12 '18 at 20:21
  • 1
    Not an answer, but a suggestion at a different solution. In this type of situation in my user interfaces, I prefer to disable buttons that should not be clicked. You could hook an event on the grid and when the row count is 0 disable the button. – G_P Jul 12 '18 at 20:27
  • The reason you're getting an object ref error on that line is likely due to that there is no current row when no rows exist - you can verify that with a break point on that line and checking the values via the quick watch. – G_P Jul 12 '18 at 20:27
  • @Nico I have updated my question and included where I am getting that error. – Noor Jul 12 '18 at 20:28
  • But is there any way to handle this situation? If the gridview is empty, then no more deletion could be possible? I can also disable the delete button but I can't communicate this validation to the delete button @G_P. – Noor Jul 12 '18 at 20:30
  • It's been a long time since I did Winforms, but you should be able to hook the UserDeletedRow event, and in the handler, check for the row count, and if no more rows exist, disable the button - more info here: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.userdeletedrow(v=vs.110).aspx – G_P Jul 12 '18 at 20:36
  • @G_P your suggestion of using CurrentRow == null works for me. Thanks – Noor Jul 12 '18 at 20:37

1 Answers1

0

My approach for preventing a user to delete a row from DataGridView if it is already empty was correct, but still I was getting an object reference not set to an instance of an object error. I was getting this error because I used Rows.Count == 0 instead of CurrentRow == 0 for validating the current row.

As suggested by @G_P, with Rows.Count, I was still receiving a row with no data. However, CurrentRow make sense for me that validated it that the currentrow is null.

Noor
  • 185
  • 2
  • 18