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