在我winform
中,我使用DataGgridView
在某些情况下,我想为某些列设置特殊的字体,我使用以下代码来实现
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
通
发布于 2011-12-01 20:00:30
您可以使用以下代码为每个单元格设置单独的样式:
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。
发布于 2011-12-23 15:13:57
也许,这不是它看起来的样子。我也有过类似的经历。我创造了一种风格
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
的引用,所有单元格都共享相同的引用。改变它们中的任何一个都会改变它们。我真不敢相信我找了这么久才明白。修复方法是为每个单元格创建一个“新”样式,这样它们就不会共享相同的引用。
发布于 2011-12-01 17:45:20
请尝试使用以下代码:
grvRequest.Rows[i].Cells[0].Style.Add("font-family","Verdana");
grvRequest.Rows[i].Cells[0].Style.Add("font-size", "14");
https://stackoverflow.com/questions/8339186
复制相似问题