Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change font size in a column in DataGridView

Tags:

c#

winforms

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):

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:

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.

like image 841
Johan Gunawan Avatar asked Mar 15 '13 13:03

Johan Gunawan


2 Answers

Ok this is what I found out. Under InitializeComponent() there is this line:

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

 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);
 }
like image 156
Johan Gunawan Avatar answered Nov 18 '22 13:11

Johan Gunawan


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

like image 41
Lainezor Avatar answered Nov 18 '22 14:11

Lainezor