Headers in a DataGridView

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
137
Programming Experience
10+
Hello everybody,

In WinForms, the DataGridView control has RowHeadersDefaultCellStyle. I get it with MiddleCentre alignment. Nice.

At execution I get all headers aligned on the left.

In Northwind database, the product table has a product Id. It is a number, the data is aligned right.
It has a product name : the data is aligned left.
Then we have three ids, they are aligned on the right.
Then we have a quantity per unit. It appears that, at least in English, this appears more readable when centered.
Then we have four columns aligned on the right, and one with just a checkbox.

Except when specified otherwise, I think it is best that the headers are aligned as the data they represent.

How do you achieve that with DataGridView ?

I could declare a format for each header cell, and then put them in a table, but then I search a property of the DataGridView to receive it ...

With other controls I remember having a header style in the properties of the column, but there I do not find it.

Also considering the formats of the headers ... I have set a large font for the data to be able to read it, but as the headers are much longer and only need to be read once, and the tips can help, a more little font could be appropriate for some of them, particularly the ids. I presume that if we can align the headers as needed then we can also display them with the good font.
 
I think you mean column headers. For each column (and row too) you can get and change the HeaderCell.Style.
C#:
Column1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
 
Hello,
Oh, really? This does not appear in the graphical interface, does it?
Well, I make a chkdsk to cope with a database problem, and then I come back.
 
OK. So, it seems the question is solved.

Let me take a while to put that in place.
 
Last edited by a moderator:
Back
Top Bottom