Exact size of DataGridView column header.

  • Thread starter Thread starter Mr. X.
  • Start date Start date
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 :)
 
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.
 
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;
 
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;
 
Back
Top