2011-12-01 92 views
2

的細胞在我winform是使用DataGgridView 在某些情況下,我想特別的字體設置爲某個列,我實現,使用下面的代碼Dynamicaly設置字體的DataGridView

this.grvInvoice.Columns["mat_Name"].DefaultCellStyle.Font = new Font("Verdana", 14); 

但我想設置某些字體和大小僅適用於某些單元格。我嘗試下面的代碼

grvRequest.Rows[i].Cells["item"].Style.Font = new Font("Verdana", 14); 

不工作。是否有可能set specific font and size dynamically to a cell of DataGridView

回答

5

您可以使用此代碼設置單獨的樣式每個單元:

DataGridViewCell cell=null; 
// Get a cell you need here 
cell.Style = new DataGridViewCellStyle() 
{ 
     BackColor = Color.White, 
     Font = new Font("Tahoma", 8F), 
     ForeColor = SystemColors.WindowText, 
     SelectionBackColor = Color.Red, 
     SelectionForeColor = SystemColors.HighlightText 
}; 

但是,如果你不能看到任何結果都可能意味着您已經在父級別上設置了某種樣式,並且該樣式會覆蓋您的樣式。

欲瞭解更多信息查看第風格繼承這篇文章:Cell Styles in the Windows Forms DataGridView Control

0

請嘗試使用下面的代碼:

grvRequest.Rows[i].Cells[0].Style.Add("font-family","Verdana"); 
grvRequest.Rows[i].Cells[0].Style.Add("font-size", "14"); 
3

也許,這看起來不像。我有類似的事件。我創建了一個風格

private System.Windows.Forms.DataGridViewCellStyle styleRed = new System.Windows.Forms.DataGridViewCellStyle(); 

,然後行

dgvOnForm.Rows[iRow].Cells[i].Style = styleRed; 

然後我想強調一個單元格中應用這種風格到每一個細胞,而不是其他。所有細胞均標有下劃線。這不是由於繼承性,有時被忽視的面向對象編程的基礎基礎。 dgvOnForm.Rows[iRow].Cells[i].Style實際上是對styleRed的引用,並且所有單元共享相同的參考。改變他們中的任何一個都改變了他們。我無法相信我搜索了這麼久,直到它在我身上發現。 Fix是爲每個單元格創建一個「新」風格,因此它們不共享相同的參考。

0

嘗試this.its工作正常

.Columns(1).DefaultCellStyle.Font =新字體( 「投石機MS」,11,FontStyle.Underline)

0

試試這個 Me.DataGridView1.Columns (1).DefaultCellStyle.Font = New Font(「Tahoma」,11)