RE: DataGridView editable combobox Left Right Arrow keys

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi John,

Thank you for your sample project! I did see the problem when I run it on
my machine.

If a DataGridViewTextBoxCell is in edit mode and we press the Left or Right
arrow key, the input cursor will be moved within this cell. However, the
Left or Right arrow key doesn't work the same in a DataGridViewComboBoxCell
when it is in edit mode. Because by default a DataGridViewComboBoxCell is
not editable at all, the DataGridView class treats textbox cells and
combobox cells differently. I'd like to say that this behavior is by design.

A workaround is derive a new class from the DataGridView class and override
the ProcessDataGridViewKey method. In the override method, we get the
current edit control and set the SelectionStart property of the control to
the value we want and then return true to indicate that we have processed
this key.

The following is a sample:

class MyDataGridView : DataGridView
{
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
if (e.KeyData == Keys.Left || e.KeyData == Keys.Right)
{
if (this.EditingControl != null)
{
if (this.EditingControl.GetType() ==
typeof(DataGridViewComboBoxEditingControl))
{
ComboBox control = this.EditingControl as ComboBox;
if (control.DropDownStyle !=
ComboBoxStyle.DropDownList)
{
switch (e.KeyData)
{
case Keys.Left:
if (control.SelectionStart > 0)
{
control.SelectionStart--;
return true;
}
break;
case Keys.Right:
if (control.SelectionStart <
control.Text.Length)
{
control.SelectionStart++;
return true;
}
break;
}
}
}
}

}

return base.ProcessDataGridViewKey(e);
}
}

The derived DataGridView works well on my side. Please try it on your form
to see if there is any problem and let me know the result.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


.



Relevant Pages

  • Re: NULLABLE datagridviewcomboboxcolumn (vbnet 2005)
    ... I suggest that you add a special row in the lookup table that is bound to ... NullValue property of a DataGridView cell is an empty string "". ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Datagridview.datasource triggers RowsAdded event twice
    ... I have a Form with one Datagridview on it. ... the form to fill the dataSet and bind it to the DataGridView. ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • RE: Esc in DataGridView on a dialog
    ... When a DataGridView control gets focused and the user presses the ESC key, ... the DataGridView exits from the edit mode if it was in edit mode and then ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • RE: Esc in DataGridView on a dialog
    ... When a DataGridView control gets focused and the user presses the ESC key, ... the DataGridView exits from the edit mode if it was in edit mode and then ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Display decrypted text in databound controls
    ... Dim row as DataGridViewRow = myGrid.Rows ... DataGridView control in the VS2005 designer and click the event icon ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.languages.vb)