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

mbm*_*sit 4 .net c# datagridview datagridviewcolumn winforms

如何设置RightToLeftDataGridViewColumn 中特定 DataGridViewCell 的属性?

kod*_*kod 5

我知道这是一个老问题,但正如其他人所说,DataGridViewCell并且DataGridViewColumn没有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;
           }
       }
    Run Code Online (Sandbox Code Playgroud)

    (代码取自CodeProject 问题。)

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