DataGrid and end edit
-
I have a simple windows form with a DataGrid bound to a dataset from a database. When the form closes (event 'Closing') I want to flush all changes made in the grid to the database. Everything is fine except the current cell...if I have not left the cell, the data is not updated. So far I have tried
private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e) { System.Windows.Forms.DataGridCell cell = this.dataGrid1.CurrentCell; DataGridColumnStyle dgc = this.dataGrid1.TableStyles[0].GridColumnStyles[cell.ColumnNumber]; bool good = this.dataGrid1.EndEdit(dgc, this.dataGrid1.CurrentRowIndex, false ); this.oleDbDataAdapter1.Update( this.dataSetFeature1 ); }
without luck. Does any one know how to get the current state of the cell into the dataset during 'Closing'? thanks in advance cje -
I have a simple windows form with a DataGrid bound to a dataset from a database. When the form closes (event 'Closing') I want to flush all changes made in the grid to the database. Everything is fine except the current cell...if I have not left the cell, the data is not updated. So far I have tried
private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e) { System.Windows.Forms.DataGridCell cell = this.dataGrid1.CurrentCell; DataGridColumnStyle dgc = this.dataGrid1.TableStyles[0].GridColumnStyles[cell.ColumnNumber]; bool good = this.dataGrid1.EndEdit(dgc, this.dataGrid1.CurrentRowIndex, false ); this.oleDbDataAdapter1.Update( this.dataSetFeature1 ); }
without luck. Does any one know how to get the current state of the cell into the dataset during 'Closing'? thanks in advance cjeIf you want help, be specific. Why is it not working? Are you geting an exception? What exception type are you getting? What message does it contain? Code similar to what you have is not only recommended in the .NET Framework SDK, but has worked for me and others in the past. More information would be helpful to help you solve your problem.
Microsoft MVP, Visual C# My Articles
-
If you want help, be specific. Why is it not working? Are you geting an exception? What exception type are you getting? What message does it contain? Code similar to what you have is not only recommended in the .NET Framework SDK, but has worked for me and others in the past. More information would be helpful to help you solve your problem.
Microsoft MVP, Visual C# My Articles
Thanks for you interest. I would have been happier if an excpetion was thrown...there are no exceptions thrown, it just simply doesn't work. The text that was entered in to the current cell was not updated to the database. All other cells with modified text were updated correctly. If I modify the text, click on another cell and then close, the text updates properly it is only when I have not left the current cell that I have the problem. When you have had this work, was it in the 'Closing' event of the form? is there a better place to do this processing? thanks
-
Thanks for you interest. I would have been happier if an excpetion was thrown...there are no exceptions thrown, it just simply doesn't work. The text that was entered in to the current cell was not updated to the database. All other cells with modified text were updated correctly. If I modify the text, click on another cell and then close, the text updates properly it is only when I have not left the current cell that I have the problem. When you have had this work, was it in the 'Closing' event of the form? is there a better place to do this processing? thanks
Does
DataGrid.CurrentCell
return anything? Also, inDataGridColumnStyle.EndEdit
, just useDataGridCell.RowNumber
instead ofDataGridCell.CurrentRowIndex
. The number of internal instructions is a little less, giving you a small increase in performance. TheClosing
event is a good place to do this, although if you're doing this in theForm
that contains yourDataGrid
, don't handle theClosing
event; simply overrideOnClosing
:protected override void OnClosing(CancelEventArgs e)
{
// Do your stuff, perhaps even setting e.Cancel if needs be
base.OnClosing(e);
}This results in much faster code, since the IL instruction
callvirt
is used instead of many calls to aMulticastDelegate
, enumeration of the delegates (handlers), and invocation of the handlers. Overriding the event handler also gives you a little more control. While you should typically call thebase
class's method, there are some times you might not want to (like not callingbase.WndProc
for a particular method in order to surpress it from being handled at all). Note that with control events, if you don't call the method on thebase
class, the event will not be fired so any other handlers won't be invoked.Microsoft MVP, Visual C# My Articles
-
Does
DataGrid.CurrentCell
return anything? Also, inDataGridColumnStyle.EndEdit
, just useDataGridCell.RowNumber
instead ofDataGridCell.CurrentRowIndex
. The number of internal instructions is a little less, giving you a small increase in performance. TheClosing
event is a good place to do this, although if you're doing this in theForm
that contains yourDataGrid
, don't handle theClosing
event; simply overrideOnClosing
:protected override void OnClosing(CancelEventArgs e)
{
// Do your stuff, perhaps even setting e.Cancel if needs be
base.OnClosing(e);
}This results in much faster code, since the IL instruction
callvirt
is used instead of many calls to aMulticastDelegate
, enumeration of the delegates (handlers), and invocation of the handlers. Overriding the event handler also gives you a little more control. While you should typically call thebase
class's method, there are some times you might not want to (like not callingbase.WndProc
for a particular method in order to surpress it from being handled at all). Note that with control events, if you don't call the method on thebase
class, the event will not be fired so any other handlers won't be invoked.Microsoft MVP, Visual C# My Articles