This is actually quite simple to do using custom coloring elements.
All you have to do is handle the DataGridView CellPainting event:
dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
And in the handler do something like this:
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex == -1) { e.Paint(e.CellBounds, DataGridViewPaintParts.All &~DataGridViewPaintParts.ContentBackground); e.Handled = true; } }
The above code is very simple - just check if the current cell is in the title bar (has an index of -1), and then I paint everything except the ContentBackground .
I only tested this on my Windows 7 machine and it looks good, it seems that the background of the content is used only for the sorting glyph - you will want to check it in the target environment to make sure that you do not need to do even more complex regular painting, so that save ContentBackground without glyph.
The width of the header cell will still contain space for the glyph. I would generally agree that since changing this becomes a bit messy, but if you have to have a width that matches the text, then something like the following works.
First set the width in the DataBindingComplete DataGridView event:
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { if (dataGridView1.AutoSizeColumnsMode == DataGridViewAutoSizeColumnsMode.AllCells) {
After you have done this, you still need to allow the autoload of the column if the cell text is longer than the header.
For this, I used the CellValueChanged event:
void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { DataGridViewColumn c = dataGridView1.Columns[e.ColumnIndex]; if (c.AutoSizeMode == DataGridViewAutoSizeColumnMode.None) { Size s = TextRenderer.MeasureText(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(), dataGridView1.Font); if (s.Width > c.Width) { c.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader; } } }