Last Change not flushed in winforms DataGrid

  • Thread starter Thread starter WineNCheese
  • Start date Start date
W

WineNCheese

I have a mouse up event handler for a bound DataGrid with a bunch of
bool checkboxes. I want to update the data source as the user checks
(there is no submit button). The code to do so is below, and it works
fine, except the last change is not committed until the next change is
made (kind of like it is not flushed). The last change I make before
quitting the app is never persisted.

I've tried the BindingManager.EndCurrentEdit() with no success.

Any pointers appreciated...


private void dataGrid1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
Point pt = dataGrid1.PointToClient(Control.MousePosition);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if( hti.Type == DataGrid.HitTestType.Cell && hti.Column>=1 &&
hti.Column<=5 )
{
// Set the value in the control
this.dataGrid1[hti.Row, hti.Column] = ! (bool)(dataGrid1[hti.Row,
hti.Column]);

// This does nothing...
BindingManagerBase bm = this.dataGrid1.BindingContext[
this.testDataSet1, "TestTable" ];
bm.EndCurrentEdit();

// Update the database.
this.sqlDataAdapter1.Update( this.testDataSet1 );
}
}
 
Hi WineNCheese,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to update database as soon as
a checkbox cell is hit. However, the last click was not updated in the app.
If there is any misunderstanding, please feel free to let me know.

I have checked your code and tried to repro it on my machine. When I set
the DataSource property of dataGrid1 to testDataSet1.Tables["TestTable"],
the behavior of my app is exactly the same as you described in your last
post. So I assume that you have get the wrong BindingManagerBase object
from the binding context.

When we get the binding context, we need to use the original data source
object as the parameter for data source. So if we use dataGrid1.DataSource
= testDataSet1.Tables["TestTable"]; to set DataSource, we have to use
this.dataGrid1.BindingContext[this.testDataSet1.Tables["TestTable"]]; to
get the binding manager base.

Here I have modified your code. It works fine now.

dataGrid1.DataSource = testDataSet1.Tables["TestTable"]; //setting the
DataSource

private void dataGrid1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
Point pt = dataGrid1.PointToClient(Control.MousePosition);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if( hti.Type == DataGrid.HitTestType.Cell && hti.Column>=1 &&
hti.Column<=5 )
{
// Set the value in the control
this.dataGrid1[hti.Row, hti.Column] = ! (bool)(dataGrid1[hti.Row,
hti.Column]);

// This does nothing...
BindingManagerBase bm =
this.dataGrid1.BindingContext[this.testDataSet1.Tables["TestTable"]];
//get binding context accordingly
bm.EndCurrentEdit();

// Update the database.
this.sqlDataAdapter1.Update( this.testDataSet1 );
}
}

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi WineNCheese,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Yes, Thanx for the response. Very sorry for not responding. I've
been off to the UK for a bit.
Although now I'm having a similar problem - see my latest post
"ColumnChanged".
 
Hi WineNCheese,

I did not see your post "Columnchanged". If you are still have problem with
flushing last change, could you please post your test result here? If you
have other questions, please feel free to open a new thread and we will try
our best to help you.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi WineNCheese,

Maybe you didn't use the email registered in MSDN. Please use your former
email "(e-mail address removed)", so that we might see your post. Thanks!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top