2011-02-16 132 views
2

我有DataGridView的問題。默認的單元格樣式有一些邊距或填充,使得所有行比我需要的高。從DataGridView刪除填充/邊距單元格

我將RowTemplate中的Height屬性設置爲一個較小的值(即15 px),但現在單元格切下了下劃線('_')等較低的符號,單元格頂部有1或2個空白像素。

如何使一個DataGridView單元格顯示與ListView(詳細信息視圖)中沒有填充/邊距的值?

radzi0_0

回答

0

I am hoping that this goes some of the way to helping you in this situation;

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)//remove padding 
{ 
    // ignore the column header and row header cells 

    if (e.RowIndex != -1 && e.ColumnIndex != -1) 
    { 
     e.PaintBackground(e.ClipBounds, true); 
     e.Graphics.DrawString(Convert.ToString(e.FormattedValue), e.CellStyle.Font, Brushes.Gray, e.CellBounds.X, e.CellBounds.Y - 2, StringFormat.GenericDefault) 
     e.Handled = true; 
    } 
} 
0

我在我的DataGridView一些類似的問題。每次我調整列的大小時,我的字符串(例如「A」)被切斷爲「A ...」之類的東西,但是字符串沒有伸出單元格外。

現在我只是發現字符串有一些奇怪的行爲與他們的界限。在字符串周圍有一個所謂的「佈局矩形」,它實際上比字符串本身更大。這意味着如果矩形伸出可寫區域(在本例中爲DataGridViewCell),字符串將被剪切或包裝。

StringFormat format = new StringFormat(StringFormatFlags.NoClip); 

該對象提供的信息,應該繪製字符串沒有這個討厭的佈局矩形是方式大。您可以使用CodeBlend所示的StringFormat

不幸的是,我沒有找到一種方法將該對象分配給一個字符串,以便我不必關心字符串是如何繪製的。