3

In my winform am using DataGgridView In some scenario i want to set special font to some column and i achieve that using following code

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

But i want to set certain font and size to some cell only. i try the following code

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

Bu it doesn't work. Is it possible to set specific font and size dynamically to a cell of DataGridView

Nithesh Narayanan
  • 11,481
  • 34
  • 98
  • 138

5 Answers5

5

You can set a separate style for each cell using this code:

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
};

But if you can't see any results that possibly means that you have set some style on a parent level and that style overrides yours.

For more info check out paragraph Style Inheritance of this article: Cell Styles in the Windows Forms DataGridView Control.

Dmitriy Konovalov
  • 1,777
  • 14
  • 14
3

Perhaps, this is not as it appears. I had a similar incident. I created a style

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

and then applied this style to each cell in the row

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

I then wanted to underline one cell, but not the others. All cells were underlined. This was not due to inheritence, by a sometimes overlooked fundamental basis for Object Oriented Programming. The dgvOnForm.Rows[iRow].Cells[i].Style was in fact a reference to styleRed, and all cells shared the same reference. Changing any one of them changed them all. I can't believe I searched so long until it dawned on me. Fix was to create a 'new' style for each cell so they did not share the same reference.

eeerahul
  • 1,629
  • 4
  • 27
  • 38
0

try this.its works fine

.Columns(1).DefaultCellStyle.Font = New Font("Trebuchet MS",11, FontStyle.Underline)

0

Try This Me.DataGridView1.Columns(1).DefaultCellStyle.Font = New Font("Tahoma", 11)

0

Please try to use below code:

grvRequest.Rows[i].Cells[0].Style.Add("font-family","Verdana");
grvRequest.Rows[i].Cells[0].Style.Add("font-size", "14");
Elias Hossain
  • 4,410
  • 1
  • 19
  • 33