T
Tomasz J
Hello Developers,
I have a problem I really do not how to correctly handle.
Very simple scenario:
DataGridView bound to BindingSource, BindingNavigator attached,
BindingSource bound to a DataTable, let's say, with just one column. Also, I
have a TextBox bound to my BindingSource and its the only column.
When there are no records, my TextBox should be disabled, when I add a new
record focus should be set on my TextBox. I accomplish that handling two
events:
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) {
this.myTextBox.Focus();
}
private void myBindingSource_PositionChanged(object sender, EventArgs e) {
this.myTextBox.Enabled = (myBindingSource.Current != null);
}
The problem:
When I delete the last record using BindingNavigator my TextBox gets
disabled and focus moves on DataGridView. But the DataGridView does not know
yet that the only row is gone. That triggers the following exception:
System.IndexOutOfRangeException: Index -1 does not have a value.
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.get_Current()
at
System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs
e)
at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell&
dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean
canCreateNewRow, Boolean validationFailureOccurred)
at System.Windows.Forms.DataGridView.OnEnter(EventArgs e)
at System.Windows.Forms.Control.NotifyEnter()
at System.Windows.Forms.ContainerControl.UpdateFocusedControl()
I can even handle deletions through code by calling:
myBindingSource.RemoveCurrent()
, but how to do this, so the DataGridView gets notified immediately, before
it accidentally receives focus?
I know I can set focus somewhere else, before disabling the TextBox, but I
am looking for some more "elegant" solution.
Thank you for any pointers.
Tomasz J
I have a problem I really do not how to correctly handle.
Very simple scenario:
DataGridView bound to BindingSource, BindingNavigator attached,
BindingSource bound to a DataTable, let's say, with just one column. Also, I
have a TextBox bound to my BindingSource and its the only column.
When there are no records, my TextBox should be disabled, when I add a new
record focus should be set on my TextBox. I accomplish that handling two
events:
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) {
this.myTextBox.Focus();
}
private void myBindingSource_PositionChanged(object sender, EventArgs e) {
this.myTextBox.Enabled = (myBindingSource.Current != null);
}
The problem:
When I delete the last record using BindingNavigator my TextBox gets
disabled and focus moves on DataGridView. But the DataGridView does not know
yet that the only row is gone. That triggers the following exception:
System.IndexOutOfRangeException: Index -1 does not have a value.
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.get_Current()
at
System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs
e)
at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell&
dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean
canCreateNewRow, Boolean validationFailureOccurred)
at System.Windows.Forms.DataGridView.OnEnter(EventArgs e)
at System.Windows.Forms.Control.NotifyEnter()
at System.Windows.Forms.ContainerControl.UpdateFocusedControl()
I can even handle deletions through code by calling:
myBindingSource.RemoveCurrent()
, but how to do this, so the DataGridView gets notified immediately, before
it accidentally receives focus?
I know I can set focus somewhere else, before disabling the TextBox, but I
am looking for some more "elegant" solution.
Thank you for any pointers.
Tomasz J