I have "Drag&Dropped" a datasource to a form as a datagridview. I can find out witch column has the focus by this code:
dgv = (DataGridView)sender; mLastBox = dgv.CurrentCell.OwningColumn.Name;
mLastBox now holds the property name bound to the column.
I now change the columnType of the first column to DataGridViewComboBoxColumn and now this FAILS:
DataGridView dgv = (DataGridView)sender; mLastBox = dgv.CurrentCell.OwningColumn.Name;
dgv.CurrentCell is simply null !
How can I find out the column name when the columnType is DataGridViewComboBoxColumn ?
|
| PSAKSor Monday, March 30, 2009 10:01 AM |
Hi PSASor, I think the exception may be occured in the witchSearchField method, right? As I can see, every control in the collection subscribed Enter event, including DataGridView control. So when the focus enter in the DataGridView, but not in anyone of the cells, the exception may occur. Try to add a "if statement" there to avoid such as case as I previously described. Let me know your test results. Best regards, Bruce Zhou Please mark the replies as answers if they help and unmark if they don't.- Marked As Answer byPSAKSor Thursday, April 02, 2009 10:11 AM
-
|
| Bruce.Zhou Thursday, April 02, 2009 9:37 AM |
Hi PSAKSor, Would you please tell me in which handler you want to get the focused column? How about to write an "if statement" to avoid the case when dgv.CurrentCell is null? Such as : If (dgv.CurrentCell!=null) { mLastBox = dgv.CurrentCell.OwningColumn.Name; } I am looking forward to your reply. Best regards, Bruce Zhou Please mark the replies as answers if they help and unmark if they don't. |
| Bruce.Zhou Tuesday, March 31, 2009 12:08 PM |
Yeah - that's right but my problem is this.
When I have focus at the DataGridViewComboBoxColumn the dgv.CurrentCell is null and if I have more DataGridViewComboBoxColumns - witch I have - then I can't find out witch column has the focus.
|
| PSAKSor Wednesday, April 01, 2009 6:00 AM |
Hi PSAKSor, Let me see how you do that. Would you please provide more code? Best regards, Bruce Zhou Please mark the replies as answers if they help and unmark if they don't. |
| Bruce.Zhou Wednesday, April 01, 2009 6:02 AM |
Hi PSAKSor, How's your problem now? Woud you need further help? Best regards, Bruce Zhou Please mark the replies as answers if they help and unmark if they don't. |
| Bruce.Zhou Thursday, April 02, 2009 8:24 AM |
I have a stringmLastBox witch holds the name of my last textbox.
WireEnterEvent is called in the form constructor after InitializeComponent();
private void WireEnterEvent(Control.ControlCollection ctls)
{
foreach (Control ctl in ctls)
{
ctl.Enter += new EventHandler(witchSearchField);
WireEnterEvent(ctl.Controls);
}
}
private void witchSearchField(object sender, EventArgs e)
{
if (sender != bindingNavigatorFind)
{
if (sender is TextBox) mLastBox = (sender as TextBox).DataBindings[0].BindingMemberInfo.BindingField;
else if (sender is DataGridView)
{
DataGridView dgv = (DataGridView)sender;
mLastBox = dgv.CurrentCell.OwningColumn.Name;
}
else mLastBox = null;
}
bindingNavigatorFind.Enabled = mLastBox != null;
}
private void KSODataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (sender is DataGridView)
{
DataGridView dgv = (DataGridView)sender;
mLastBox = dgv.CurrentCell.OwningColumn.Name;
}
}
|
| PSAKSor Thursday, April 02, 2009 8:58 AM |
Hi PSASor, I think the exception may be occured in the witchSearchField method, right? As I can see, every control in the collection subscribed Enter event, including DataGridView control. So when the focus enter in the DataGridView, but not in anyone of the cells, the exception may occur. Try to add a "if statement" there to avoid such as case as I previously described. Let me know your test results. Best regards, Bruce Zhou Please mark the replies as answers if they help and unmark if they don't.- Marked As Answer byPSAKSor Thursday, April 02, 2009 10:11 AM
-
|
| Bruce.Zhou Thursday, April 02, 2009 9:37 AM |