C# 更改 DataGridView 列中的字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15433731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change font size in a column in DataGridView
提问by Johan Gunawan
I have a column in a DataGridView (WinForm application) that needs the font size and style changed. From the article here: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.font.aspx, I am thinking that the code below will get the result that I want (I am testing by changing the styling first):
我在 DataGridView(WinForm 应用程序)中有一个需要更改字体大小和样式的列。从这里的文章:http: //msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.font.aspx,我想下面的代码会得到我想要的结果(我是首先通过更改样式进行测试):
this.dataGridViewMain.Columns[3].DefaultCellStyle.Font = new Font(dataGridViewMain.DefaultCellStyle.Font, FontStyle.Italic);
But the code does not change anything. I also tried to add the code on the RowPostPaint
event handler but still does not work. I know the font that is used by the program is set on the DataGridView.RowsDefaultCellStyle
properties but I thought placing code in the RowPostPaint
event will override that. Below is the code from the RowPostPaint
event:
但是代码不会改变任何东西。我也尝试在RowPostPaint
事件处理程序上添加代码,但仍然不起作用。我知道程序使用的字体是在DataGridView.RowsDefaultCellStyle
属性上设置的,但我认为在RowPostPaint
事件中放置代码会覆盖它。以下是活动中的代码RowPostPaint
:
void dataGridViewMain_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
this.dataGridViewMain.Columns[3].DefaultCellStyle.BackColor = Color.Gray;
foreach (DataGridViewRow row in this.dataGridViewMain.Rows)
{
int daysInShop = Convert.ToInt32(row.Cells["Days in the shop"].Value);
if (daysInShop > 4)
{
row.DefaultCellStyle.BackColor = Color.Red;
row.DefaultCellStyle.ForeColor = Color.White;
}
else if (daysInShop > 2)
{
row.DefaultCellStyle.BackColor = Color.Yellow;
}
else
{
row.DefaultCellStyle.BackColor = Color.YellowGreen;
}
row.Height = 35;
}
this.dataGridViewMain.CurrentCell = null; // no row is selected when DGV is displayed
}
Any help is appreciated. Thanks.
任何帮助表示赞赏。谢谢。
回答by Lainezor
Font size is read only so you would want to make a new font and set yourDataGridView.Font = new Font(name,size,style) here is more info: http://msdn.microsoft.com/en-us/library/system.drawing.font.aspx
字体大小是只读的,因此您需要创建新字体并设置 yourDataGridView.Font = new Font(name,size,style) 这里是更多信息:http: //msdn.microsoft.com/en-us/library/ system.drawing.font.aspx
回答by Johan Gunawan
Ok this is what I found out. Under InitializeComponent()
there is this line:
好的,这就是我发现的。在InitializeComponent()
有这一行:
dataGridViewCellStyle3.Font = new System.Drawing.Font("Verdana", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
When I comment that line out, then the code to italicize a column that is in RowPostPaint
works fine. I then added the code below in the RowPostPaint
so that other columns font are bold and have smaller size. I am still not quite sure why DataGridView.Columns[colNumber].DefaultCellStyle.Font
does not override dataGridViewCellStyle3
当我注释掉该行时,用于斜体的代码可以RowPostPaint
正常工作。然后我在下面添加了代码,RowPostPaint
以便其他列字体为粗体且尺寸更小。我仍然不太确定为什么DataGridView.Columns[colNumber].DefaultCellStyle.Font
不覆盖dataGridViewCellStyle3
int colCount = dataGridViewMain.ColumnCount;
for (int i = 0; i < colCount; i++)
{
if(i != 3)
this.dataGridViewMain.Columns[i].DefaultCellStyle.Font = new System.Drawing.Font("Verdana", 14F, FontStyle.Bold);
else
this.dataGridViewMain.Columns[3].DefaultCellStyle.Font = new System.Drawing.Font("Verdana", 25F, FontStyle.Bold);
}
回答by Jespa
Set RowsDefaultCellStyle
to null
after InitializeComponent()
.
设置RowsDefaultCellStyle
为null
之后InitializeComponent()
。
I think the DataGridView
takes styles in the order grid/column/row. So if a row style is set it always overrides any column style.
我认为DataGridView
采用网格/列/行顺序的样式。因此,如果设置了行样式,它总是会覆盖任何列样式。
In my view this is poor design - there is no need for a default row style at all!
在我看来,这是一个糟糕的设计——根本不需要默认的行样式!