Exact size of DataGridView column header.

M

Mr. X.

Hello.
I need to calculate the exact size of DataGridView column header.
Suppose I have a title on it : "my title".
I did the calculation :

dim g as graphics
....
g = createGraphics()
thesize = g.measureString("my title", myDataGridView.font)
(is myDataGridView.font correct for the above?)
But also the above is not enough :
There is a space gap that the header has (at the left of the title, and at
the right),
but I don't know how to resolve it.

Thanks :)
 
A

Armin Zingler

Am 09.07.2010 13:01, schrieb Mr. X.:
Hello.
I need to calculate the exact size of DataGridView column header.
Suppose I have a title on it : "my title".
I did the calculation :

dim g as graphics
...
g = createGraphics()
thesize = g.measureString("my title", myDataGridView.font)
(is myDataGridView.font correct for the above?)
But also the above is not enough :
There is a space gap that the header has (at the left of the title, and at
the right),
but I don't know how to resolve it.

Thanks :)

The layout of the header cells is not documented and there is no public
method to retrieve the width. You could call

dgv.AutoResizeColumn(column, DataGridViewAutoSizeColumnMode.ColumnHeader)

and get the column's width afterwards, but that's an "invasive" method
that does not only measure.

However, I think you won't need it anymore because the AutoResizeColumn
method mentioned in my prev reply should do whole task.
 
D

DuboisP

Am 09.07.2010 13:01, schrieb Mr. X.:

The layout of the header cells is not documented and there is no public
method to retrieve the width. You could call

dgv.AutoResizeColumn(column,
DataGridViewAutoSizeColumnMode.ColumnHeader)

and get the column's width afterwards, but that's an "invasive" method
that does not only measure.

However, I think you won't need it anymore because the AutoResizeColumn
method mentioned in my prev reply should do whole task.

in another language, Borland Delphi
The TEXTMETRIC structure contains basic information about a physical font


nAverage := AveCharWidth( Canvas.Handle);
ColWidths[ xxxxx ] := nAverage * NumberOfCaractersInTheTitleOrWhatYouWant;


Function AveCharWidth( nHandle : HDC) : Word;

Var pMetrics : ^tTextMetric;
nTaille : LongInt;

Begin
nTaille := SizeOf( tTextMetric);
GetMem( pMetrics, nTaille);
If GetTextMetrics( nHandle, pMetrics^) Then
AveCharWidth := pMetrics^.tmAveCharWidth
Else
AveCharWidth := 7;
FreeMem( pMetrics, nTaille);
End;
 
M

Mr. X.

Oh ...
I remember this long long time ago.

DuboisP said:
Am 09.07.2010 13:01, schrieb Mr. X.:

The layout of the header cells is not documented and there is no public
method to retrieve the width. You could call

dgv.AutoResizeColumn(column,
DataGridViewAutoSizeColumnMode.ColumnHeader)

and get the column's width afterwards, but that's an "invasive" method
that does not only measure.

However, I think you won't need it anymore because the AutoResizeColumn
method mentioned in my prev reply should do whole task.

in another language, Borland Delphi
The TEXTMETRIC structure contains basic information about a physical font


nAverage := AveCharWidth( Canvas.Handle);
ColWidths[ xxxxx ] := nAverage * NumberOfCaractersInTheTitleOrWhatYouWant;


Function AveCharWidth( nHandle : HDC) : Word;

Var pMetrics : ^tTextMetric;
nTaille : LongInt;

Begin
nTaille := SizeOf( tTextMetric);
GetMem( pMetrics, nTaille);
If GetTextMetrics( nHandle, pMetrics^) Then
AveCharWidth := pMetrics^.tmAveCharWidth
Else
AveCharWidth := 7;
FreeMem( pMetrics, nTaille);
End;
 

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