1
votes

I am trying to delete a user through his ID by selecting a cell/row in the DataGridview and then clicking on the delete button. I tried to get the ID by using the row index and column index. However, I am not able to delete the user as the value of 'string id' is NULL even when my datagridview contains rows.

private void buttonDeleteUser_Click(object sender, EventArgs e)
{
   int rowindex = dataGridView1.CurrentCell.RowIndex;
   int columnindex = 0;
   string id = dataGridView1[rowindex, columnindex].Value.ToString();
   controller_employee.delete_employee(Int32.Parse(id));
}

Why is it NULL and what can I do to resolve this?

1
what index are you getting from dataGridView1.CurrentCell.RowIndex - Hary
The currentrow index for the row that I have selected in the datagridview - user1779026
Have you debugged with breakpoints? what id are you getting from string id = dataGridView1[rowindex, columnindex].Value.ToString(); - Hary
Hey, string id gives me null while rowindex is 7 and columnindex is 0. Value returns me one of the user's name - user1779026
Is the ID part of your DataGridView as a non visible field..? what does your query look like that returns the rows that populate the DataGridView..? - MethodMan

1 Answers

1
votes
ID = dataGridView.SelectedCells[0].RowIndex;

Here SelectedCells[0] specifies the column no.

And try selecting for whole rows rather than a single cell