0

I am developing a windows form application for a Books Store. In this application have a datagridview to select books for sale. If there are no items available in the stock I need to clear the current row values.So how can I do this. I tried this

dataGridView1.Rows.Clear();

But this clear entire datagridview. But I need to clear only current row when I click ok the message.

enter image description here

Kith
  • 117
  • 3
  • 17

2 Answers2

0

You will need to specify the row in the datagrid that you want to delete. You can do this by selecting the "index" of the row which is selected.

dataGrid.Rows[index].Selected = true;

or you could do the following:

dataGrid.SelectedRows.Clear();
foreach(DataGridViewRow row in dataGrid.Rows)
{
    if(YOUR CONDITION)
       row.Selected = true;
}
Fuzzybear
  • 1,388
  • 2
  • 25
  • 42
0

I always use removeat by first checking to ensure something is selected and then do a removeat while passing the index.

if(dataGridView1.CurrentRow.Index > -1 && !dataGridView1.Rows[dataGridView1.CurrentRow.Index].IsNewRow)
    dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);

EDIT: Added a check for IsNewRow in case AllowUsersToAddRows is True which would throw and exception if that was the selected row.

Charles May
  • 1,725
  • 1
  • 11
  • 18