PC Review


Reply
Thread Tools Rate Thread

Anyone have a good solution to the following problem

 
 
Tony Johansson
Guest
Posts: n/a
 
      8th May 2009
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


 
Reply With Quote
 
 
 
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      8th May 2009
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" <(E-Mail Removed)> wrote in message
news:WaWMl.8586$(E-Mail Removed)...
> 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
>


 
Reply With Quote
 
Tony Johansson
Guest
Posts: n/a
 
      8th May 2009
Hello!

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

//Tony



"Cor Ligthert[MVP]" <(E-Mail Removed)> skrev i meddelandet
news:O%2364QJ%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:WaWMl.8586$(E-Mail Removed)...
>> 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
>>

>



 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      8th May 2009
Tony,

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

http://msdn.microsoft.com/en-us/libr...faultview.aspx

Cor

"Tony Johansson" <(E-Mail Removed)> wrote in message
news:d_WMl.8590$(E-Mail Removed)...
> Hello!
>
> I don't quite understand what you mean.
> Can you give a simple example.
>
> //Tony
>
>
>
> "Cor Ligthert[MVP]" <(E-Mail Removed)> skrev i meddelandet
> news:O%2364QJ%(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:WaWMl.8586$(E-Mail Removed)...
>>> 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
>>>

>>

>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use a temporary table - is this a good solution to the problem? Steve Microsoft Access Database Table Design 4 11th Apr 2009 12:39 AM
Dictionary Problem solved..but is it a good solution? David H Microsoft C# .NET 7 30th Jul 2008 03:40 PM
Good reporting solution Jay Douglas Microsoft ASP .NET 3 23rd Feb 2004 08:25 PM
any good solution RAMESH Windows XP Performance 2 22nd Feb 2004 06:56 PM
solution - be good out there!! Nate Windows XP Security 0 20th Aug 2003 08:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:25 PM.