DataGridView question

A

Alex K.

Hi all

No matter how hard I try, I cannot force my DataGridView to use specific
font in column headers: it seems to be always the same as container's font
(which is form in my case).
I set styles:
ColumnHeadersDefaultCellStyle
DefaultCellStyle

and even
AlternatingRowsDefaultCellStyle
and
RowsDefaultCellStyle

making sure thay all use specific font /size, but at run time column headers
appear in sans serif 10pt, which is the same as form's font.

What's wrong?

Is it because I am assigning DataSource on the fly without adding columns at
design time?

Thank you
 
C

Ciaran O''Donnell

This works for me. I made a form and dragged a datagridview on and put this
in the code:

private void frmTest_Load(object sender, EventArgs e)
{

DataTable table = new DataTable("My Table");
table.Columns.Add(new DataColumn("Col 1", typeof(string)));
table.Columns.Add(new DataColumn("Col 2", typeof(string)));
table.Columns.Add(new DataColumn("Col 3", typeof(string)));
table.Columns.Add(new DataColumn("Col 4", typeof(string)));

dataGridView1.ColumnAdded += new
DataGridViewColumnEventHandler(dataGridView1_ColumnAdded);
dataGridView1.DataSource = table;



}

void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
e.Column.HeaderCell.Style = new DataGridViewCellStyle();
e.Column.HeaderCell.Style.Font = new System.Drawing.Font("Arial", 14,
FontStyle.Underline);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top