如何设置 DataGridViewCell 中字符串的方向?

发布于 2024-10-17 19:31:27 字数 83 浏览 6 评论 0原文

如何为 DataGridViewColumn 中的特定 DataGridViewCell 设置 RightToLeft 属性?

How do I set the RightToLeft property for a particular DataGridViewCell in a DataGridViewColumn?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遇到 2024-10-24 19:31:27

我知道这是一个老问题,但正如其他人所说, DataGridViewCell 和 DataGridViewColumn 没有 RightToLeft 属性。但是,有解决此问题的方法:

  1. 处理CellPainting 事件并使用 TextFormatFlags.RightToLeft 标志:

    private void RTLColumnsDGV_CellPainting(对象发送者,DataGridViewCellPaintingEventArgs e)
       {
          if (e.ColumnIndex == RTLColumnID && e.RowIndex >= 0)
          {
             e.PaintBackground(e.CellBounds, true);
              TextRenderer.DrawText(e.Graphics, e.FormattedValue.ToString(),
              e.CellStyle.Font、e.CellBounds、e.CellStyle.ForeColor、
               TextFormatFlags.RightToLeft | TextFormatFlags.RightToLeft | TextFormatFlags.Right);
              e.已处理=真;
           }
       }

    (代码取自CodeProject问题.)

  2. 如果 DGV 中只有一个特定单元格,您可以尝试插入不可见的 CodeProject 问题。 wikipedia.org/wiki/Right-to-left_mark" rel="noreferrer">RTL 字符 (U+200F) 位于单元格内容的开头。

I know it's an old question, but as others said, DataGridViewCell and DataGridViewColumn don't have the RightToLeft property. However, there are workarounds to this problem:

  1. Handle the CellPainting event and use the TextFormatFlags.RightToLeft flag:

    private void RTLColumnsDGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
       {
          if (e.ColumnIndex == RTLColumnID && e.RowIndex >= 0)
          {
             e.PaintBackground(e.CellBounds, true);
              TextRenderer.DrawText(e.Graphics, e.FormattedValue.ToString(),
              e.CellStyle.Font, e.CellBounds, e.CellStyle.ForeColor,
               TextFormatFlags.RightToLeft | TextFormatFlags.Right);
              e.Handled = true;
           }
       }

    (Code taken from CodeProject question.)

  2. If it's only one particular cell in the DGV, you may try to insert the invisible RTL character (U+200F) in the beginning of the cell content.

邮友 2024-10-24 19:31:27

这样的属性不存在。您需要设置 RightToLeft整个控件的 code> 属性

我怀疑您试图滥用该属性来右对齐您的文本。它旨在支持使用从右到左字体的区域设置,而不是自定义格式。

如果您的目标是更改格式,则每个 DataGridViewCell 都有一个 Style 属性,接受 DataGridViewCellStyle。您可以设置其对齐 属性 设置为“MiddleRight”,将单元格内容垂直居中对齐,水平居右对齐。有关详细信息,请参阅:如何:设置 Windows 窗体 DataGridView 控件中的数据格式。

Such a property does not exist. You need to set the RightToLeft property of the entire control.

I suspect that you're attempting to mis-use the property to right-justify your text. It's intended to enable support for locales that use right-to-left fonts, not for custom formatting.

If altering formatting is your goal, each DataGridViewCell has a Style property that accepts an instance of the DataGridViewCellStyle class. You can set its Alignment property to "MiddleRight" to align the content of the cell vertically at the middle and horizontally at the right. For more information, see: How to: Format Data in the Windows Forms DataGridView Control.

简单爱 2024-10-24 19:31:27

要对整个列执行此操作,请使用

dataGridView.Columns["column name"].DefaultCellStyle.Alignment = DataGridViewAlignment.MiddleRight;

虽然我相信单个单元格的样式将覆盖它。

To do so for the whole column, use

dataGridView.Columns["column name"].DefaultCellStyle.Alignment = DataGridViewAlignment.MiddleRight;

Although I believe the individual cell's style will override this.

思念绕指尖 2024-10-24 19:31:27

就这么简单:

DataGridView1.Columns["name of column"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

As simple as that:

DataGridView1.Columns["name of column"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
柒夜笙歌凉 2024-10-24 19:31:27

我知道这是一篇相当旧的帖子,但很多时候我找到旧帖子的答案同样有助于为我指出解决方案,所以无论如何我都会发布我的解决方案。

我通过处理 datagridview 的 EditingControlShowing 事件来做到这一点。在解决这个问题时,让我困惑的一件事是我试图在 datagridviewcell 中寻找 RightToLeft 属性,但这是 Textbox 的属性。

private void MyDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        TextBox currentCell = e.Control as TextBox;
        if (currentCell != null
            && myDataGridView.CurrentCell.ColumnIndex == NameOfYourColumn.Index) //or compare using column name
        {
            currentCell.RightToLeft = RightToLeft.Yes;
        }
    }

I know this is quite an old post, but many times I have found answers to an old post just as helpful to point me to a solution, so I will post my solution anyway.

I did this by handling the EditingControlShowing event of the datagridview. One thing that was throwing me off when solving this was that I was trying to look for a property RightToLeft in a datagridviewcell, however that is a property of Textbox instead.

private void MyDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        TextBox currentCell = e.Control as TextBox;
        if (currentCell != null
            && myDataGridView.CurrentCell.ColumnIndex == NameOfYourColumn.Index) //or compare using column name
        {
            currentCell.RightToLeft = RightToLeft.Yes;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文