Anyone have a good solution to the following problem

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
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
 
Tony,

Use the defaultview instead of direct the table.

The defaultview is a property of the datatable from the Type Dataview.

Use that defaultview as well for everything (by instance the binding).

It is simple dt.Defaultview (a row of the defaultview (dataview) is the
DataRowView)

Cor
 
Hello!

I don't quite understand what you mean.
Can you give a simple example.

//Tony



Cor Ligthert said:
Tony,

Use the defaultview instead of direct the table.

The defaultview is a property of the datatable from the Type Dataview.

Use that defaultview as well for everything (by instance the binding).

It is simple dt.Defaultview (a row of the defaultview (dataview) is the
DataRowView)

Cor

Tony Johansson said:
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
 
Tony,

There is to much about the defaultview, you can first start with looking at
this.

http://msdn.microsoft.com/en-us/library/system.data.datatable.defaultview.aspx

Cor

Tony Johansson said:
Hello!

I don't quite understand what you mean.
Can you give a simple example.

//Tony



Cor Ligthert said:
Tony,

Use the defaultview instead of direct the table.

The defaultview is a property of the datatable from the Type Dataview.

Use that defaultview as well for everything (by instance the binding).

It is simple dt.Defaultview (a row of the defaultview (dataview) is the
DataRowView)

Cor

Tony Johansson said:
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
 
Back
Top