-1

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

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

无论如何,我尝试根据要编辑的列的名称/数据类型在 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 属性会导致当您编辑单元格值时,在第一次单元格编辑后,每次更改下一个单元格值都会恢复为原始值!

我不明白为什么会这样?!

4

3 回答 3

0

您可以使用:

 this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight;
于 2015-03-30T06:38:00.483 回答
0

更改 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;
}

可能是一个错误:|

于 2015-09-04T19:44:21.050 回答
0

作为一种解决方法,我发送CTRL+Left-Shift表示从左到右的方向,并在事件中发送CTRL+Right-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);
    }
于 2018-01-08T09:35:54.910 回答