2011-02-17 21 views
3

如何爲DataGridViewColumn中的特定DataGridViewCell設置RightToLeft屬性?如何在DataGridViewCell中設置字符串的方向?

+1

我需要設置方向不對齊的單元格 – mbmsit 2011-02-17 09:50:48

+0

這是什麼意思?我不確定我是否理解這個區別。正如我的答案所解釋的,您不能爲單個單元格設置RightToLeft屬性;它只適用於整個控制。它是爲使用從右到左的字體的區域設計的,在這種情況下,屏幕上的所有內容都需要從右到左,而不僅僅是一個特定的單元格。 「方向」與「對齊」有何不同? – 2011-02-19 04:35:05

回答

2

這樣的屬性不存在。您需要設置整個控件RightToLeft property

我懷疑你試圖誤用這個屬性來對你的文本進行正確的對齊。它旨在支持使用從右到左字體的區域設置,而不是自定義格式。

如果更改格式是您的目標,則每個DataGridViewCell都有一個Style property,它接受DataGridViewCellStyle class的實例。您可以將Alignment property設置爲「MiddleRight」,以便在中間垂直對齊單元的內容,並在右側水平對齊。有關更多信息,請參閱:How to: Format Data in the Windows Forms DataGridView Control

1

爲了整個列爲此,使用

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

雖然我相信個別單元格樣式將覆蓋此。

3

我知道這是一個老問題,但正如其他人所說,DataGridViewCellDataGridViewColumn沒有RightToLeft屬性。不過,也有方法可以解決該問題:

  1. 處理的CellPainting事件並使用TextFormatFlags.RightToLeft標誌:(代碼從CodeProject question拍攝)

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

  2. 如果這只是一個特定的您可能會嘗試在單元格內容的開頭插入不可見的RTL character(U + 200F)。

1

就這麼簡單:

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

我知道這是一個很老的文章,但很多時候我已經找到答案,舊的文章,就像有幫助的指點一個解決方案,所以我會發布我的解決方案。

我通過處理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; 
     } 
    } 
相關問題