【问题标题】:How can I set the direction of a string in a DataGridViewCell?如何在 DataGridViewCell 中设置字符串的方向?
【发布时间】:2011-06-28 22:12:15
【问题描述】:

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

【问题讨论】:

  • 我需要为单元格设置方向而不是对齐
  • 这是什么意思?我不确定我是否理解这种区别。正如我的回答所解释的,您不能为单个单元格设置 RightToLeft 属性;它仅适用于整个控件。它是为使用从右到左字体的语言环境设计的,在这种情况下,屏幕上的所有内容都需要从右到左,而不仅仅是一个特定的单元格。 “方向”与“对齐”有何不同?
  • (我知道这是旧的)使用从右到左的字体并不一定意味着屏幕上的所有内容都需要从右到左。例如,您可以有一个 DataGridView 以不同的语言显示单词的翻译。显示 RTL 语言的列应显示为 RTL。似乎当使用.net DataGridView 来实现这一点时,必须将单元格字体设置为 RTL 并将 CellStyle.Alignment 设置为 DataGridViewContentAlignment.MiddleRight。 (尽管这似乎不适用于单声道 Winform)
  • 这能回答你的问题吗? RightToLeft DatagridviewCell

标签: c# .net winforms datagridview datagridviewcolumn


【解决方案1】:

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

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

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

    (代码取自CodeProject question。)

  2. 如果只是 DGV 中的一个特定单元格,您可以尝试在单元格内容的开头插入不可见的RTL character (U+200F)。

【讨论】:

  • 如果不想进行自定义绘画,则此答案中的第 2 点是最佳选择。与其他答案所说的不同,设置单元格的Alignment 与将其方向设置为 RTL 不同。
【解决方案2】:

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

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

如果您的目标是更改格式,则每个DataGridViewCell 都有一个Style property,它接受DataGridViewCellStyle class 的一个实例。您可以将其Alignment property 设置为“MiddleRight”,以使单元格的内容垂直居中对齐,水平居右对齐。如需更多信息,请参阅:How to: Format Data in the Windows Forms DataGridView Control

【讨论】:

    【解决方案3】:

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

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

    虽然我相信单个单元格的样式会覆盖这一点。

    【讨论】:

      【解决方案4】:

      就这么简单:

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

      【讨论】:

        【解决方案5】:

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

        我通过处理 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;
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多