设置EditControl的RightToLeft属性时DataGridView的奇怪行为

时间:2013-09-24 05:17:54

标签: c# datagridview right-to-left

我在WindowsForms应用程序中有一个DataGridView,其RightToLeft属性设置为true(RightToLeft.Yes),因为rtl语言中的文本需要这样。另一方面,数值应该从左到右书写。

但是您可能知道,我们无法为各个列设置不同的RightToLeft值。我想知道为什么这个被认为用于大多数数据应用程序的重要控件不支持这个基本功能用于国际目的。这是必需的

无论如何,我尝试根据要编辑的列的名称/ dataType在EditingControlShowing事件中更改RightToLeft属性:

    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;

    }

但是这不能按预期工作。编辑EditingControl的RightToLeft属性会导致在编辑单元格值时,在第一次单元格编辑后,每次更改单元格值时,下一个单元格值将恢复为原始值!

我无法弄清楚为什么会发生这种情况?!

3 个答案:

答案 0 :(得分:0)

您可以使用:

 this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight;

答案 1 :(得分:0)

作为一种解决方法,我从左到右方向发送 CTRL + 左移,并且 CTRL + 右-Shift 表示EditingControlShowing事件中从右到左的方向。

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);
    }

答案 2 :(得分:-1)

更改e.Control的Text属性将解决问题! 我很困惑你! 但这很好用:

private void EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    string temp = e.Control.Text;
    e.Control.Text += " ";
    e.Control.Text = temp;

    if (some_condition)
        e.Control.RightToLeft = System.Windows.Forms.RightToLeft.No;
    else
        e.Control.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
}

可能是个错误:|