T
Tony Johansson
Hello!
Here is the code which is located in the event method BtnDelete_Click
listed below.
I have a DataGridView which is bound to a DataTable in the following way
_bindingSource.DataSource = _dataTable;
dgStock.DataSource = _bindingSource;
The DataGridView is called dgStock and the DataTable is called _dataTable
and the
BindingSource is called _bindingSource.
Now to the real problem
Assume that you have three rows in the DataGridView and you select the first
row and then
press the delete button so the event handler BtnDelete_Click is called. This
event handler
will delete the selected row so the row in the DataGridView will be removed
which is correct.
Now you have two rows left in the DataGridView. You select the first row
again and press the delete
button. But now the first row will not be removed because row[0] has already
been deleted.
The variable_currentRow is a DataTable object located as a class instance
This variable currentRow is set in the event method
dgStock_SelectionChanged.
So does anyone have a good solution how I would solve my problem.
private void dgStock_SelectionChanged(object sender, EventArgs e)
{
_currentRow = dgStock.CurrentRow;
}
private void BtnDelete_Click(object sender, EventArgs e)
{
int quantityInStock;
DataRow row;
if (_currentRow.Index < _dataTable.Rows.Count)
{
row = _dataTable.Rows[_currentRow.Index];
if (row.RowState != DataRowState.Deleted &&
int.TryParse(row[ColumnName.Count.ToString()].ToString(), out
quantityInStock) && quantityInStock > 0)
{
DialogResult dr = MessageBox.Show(this, "Do you really want
to remove the product when we have " + quantityInStock.ToString() + " in
stock", "Quantity in Stock check", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
row.Delete();
}
else
row.Delete();
}
}
//Tony
Here is the code which is located in the event method BtnDelete_Click
listed below.
I have a DataGridView which is bound to a DataTable in the following way
_bindingSource.DataSource = _dataTable;
dgStock.DataSource = _bindingSource;
The DataGridView is called dgStock and the DataTable is called _dataTable
and the
BindingSource is called _bindingSource.
Now to the real problem
Assume that you have three rows in the DataGridView and you select the first
row and then
press the delete button so the event handler BtnDelete_Click is called. This
event handler
will delete the selected row so the row in the DataGridView will be removed
which is correct.
Now you have two rows left in the DataGridView. You select the first row
again and press the delete
button. But now the first row will not be removed because row[0] has already
been deleted.
The variable_currentRow is a DataTable object located as a class instance
This variable currentRow is set in the event method
dgStock_SelectionChanged.
So does anyone have a good solution how I would solve my problem.
private void dgStock_SelectionChanged(object sender, EventArgs e)
{
_currentRow = dgStock.CurrentRow;
}
private void BtnDelete_Click(object sender, EventArgs e)
{
int quantityInStock;
DataRow row;
if (_currentRow.Index < _dataTable.Rows.Count)
{
row = _dataTable.Rows[_currentRow.Index];
if (row.RowState != DataRowState.Deleted &&
int.TryParse(row[ColumnName.Count.ToString()].ToString(), out
quantityInStock) && quantityInStock > 0)
{
DialogResult dr = MessageBox.Show(this, "Do you really want
to remove the product when we have " + quantityInStock.ToString() + " in
stock", "Quantity in Stock check", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
row.Delete();
}
else
row.Delete();
}
}
//Tony