1

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?

Incredible
  • 3,495
  • 8
  • 49
  • 77
user1779026
  • 93
  • 3
  • 8
  • what `index` are you getting from `dataGridView1.CurrentCell.RowIndex` – Hary Jan 17 '13 at 04:09
  • The currentrow index for the row that I have selected in the datagridview – user1779026 Jan 17 '13 at 04:11
  • Have you `debugged` with `breakpoints`? what id are you getting from `string id = dataGridView1[rowindex, columnindex].Value.ToString();` – Hary Jan 17 '13 at 04:13
  • Hey, string id gives me null while rowindex is 7 and columnindex is 0. Value returns me one of the user's name – user1779026 Jan 17 '13 at 04:26
  • 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 Jan 17 '13 at 04:35
  • It is visible. I changed to `string id = dataGridView1[columnindex, rowindex].Value.ToString();`. the Value returns the correct ID BUT it does not get passed into string id – user1779026 Jan 17 '13 at 05:03
  • Check out this post best answer I have found so far: [http://stackoverflow.com/questions/16701943/how-to-identify-datagridview-cell-that-was-right-clicked-on-for-contextmenustrip][1] – jkdba Sep 02 '14 at 18:46

1 Answers1

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

Here SelectedCells[0] specifies the column no.

And try selecting for whole rows rather than a single cell

Sandip Bantawa
  • 2,822
  • 4
  • 31
  • 47