I have a DataGridView in a WindowsForms app with RightToLeft property set to true (RightToLeft.Yes) as texts in rtl languages required this. in the other hand, the numeric values should be written as left to right.
But As you may know, we can not set different values of RightToLeft for individual Columns. I'm wondering why such an important control which is considered to be used for most Data App's does not support this basic feature for international purposes. this is required
anyway, i try to change the RightToLeft property at EditingControlShowing event based on the name/dataType of the column to be edited:
private void grid1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox t = e.Control as TextBox;
if (t == null) return; //it is not a textbox editing column
DataGridViewColumn col = grid1.Columns[grid1.CurrentCell.ColumnIndex];
string columnName = col.Name;
if (Utility.IsNumericType(col.ValueType) || columnName.Contains("تاریخ")) //render as left-to-right
t.RightToLeft = RightToLeft.No; //setting RightToLeft property causes that cell values do not update after first cell edit!!!
else
t.RightToLeft = RightToLeft.Yes;
}
but this does not work as expected. setting RightToLeft property of the EditingControl causes that when you are editing cell values, after first cell edit, next cell values revert to their original values every time you change them!
i can't figure out why this happens?!
As a workaround, i send CTRL+Left-Shift for Left-To-Right direction, and CTRL+Right-Shift for Right-to-Left direction in EditingControlShowing
event.
if (Utility.IsNumericType(column.ValueType) || IsDateColumn(columnName)) //render as left-to-right
KeyboardUtility.KeyPressByHold(Keys.ControlKey, Keys.LShiftKey); //CTRL+LeftShift = Left-To-Right direction
else //render as right-to-left
KeyboardUtility.KeyPressByHold(Keys.ControlKey, Keys.RShiftKey); //CTRL+RightShift = Right-To-Left direction
--------------------------
public static void KeyPressByHold(Keys holdKey, Keys pressKey)
{
KeyDown(holdKey); // => keybd_event(key, flag);
KeyPress(pressKey);
KeyUp(holdKey);
}
/// <param name="bVirtualKey">Virtual Keycode of keys. E.g. VK_RETURN, VK_TAB, … You may use Keys enumeration in .Net for this.</param>
/// <param name="bScanCode">Scan Code value of the keys (scan code is a hardware dependent code based on the model of the keyboard). E.g. 0xb8 for “Left Alt” key.</param>
/// <param name="dwFlags">Flag that is set for key state. E.g. KEYEVENTF_KEYUP or KEYEVENTF_EXTENDEDKEY</param>
/// <param name="dwExtraInfo">32-bit extra information about keystroke.</param>
public static void keybd_event(Keys VirtualKey, KeyEventF dwFlags) // bool flagKeyUp, bool flagExtendedKey) //this version is more easy to call in .Net
{
uint ScanCode = MapVirtualKey((uint)VirtualKey, 0); // 0 = VirtualKey to ScanCode
keybd_event((byte)VirtualKey, (byte)ScanCode, (uint)dwFlags, 0);
}