Programmatically size a DataGridView

Hi,

I posted this in the 'windows forms data controls & databinding forum' but didn't get any response -- so am trying here as well.

i'd like to set up a DataGridView with:

(a) just a horizontal scroll bar at design time

(b) porgrammatically size it vertically to accomodate all rows plus the column header, unless the number rows exceeds N, in which case, programmatically add a vertical scroll bar. The datagridview will not be bound to any data-source. Instead i programmatically add rows to its (initially empty) DataGridViewRowCollection.

I have experimented with trying to programmatically determine the height to set it to with the following code:

dgv.height = dgv.rows.GetRowsHeight(DataGridViewElementStates.None) + dgv.columnHeadersHeight

// where dgv is my DataGridView instance

Anyway, this does not seem to work - it falls short of the height it should be.

As such, any idea how i'd do (b)?

Thanks,

Mark

[1672 byte] By [Mark123] at [2007-12-22]
# 1
Is is possible that it is off just by some constant value (like the size of the borders, etc)? Then you could just add the constant value to get the correct height.
DeborahK at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic IDE...
# 2
I wrote this to dynamically resize the width of the datagridView control

Private Sub ResizeDgVarGrid()
Dim iWidth As Integer = 0
Dim maxWidth as Integer=527
For i As Integer = 0 To dgVarData.Columns.Count - 1
iWidth += dgVarData.Columns(i).Width
Next
If (iWidth + 50) >=
maxWidth Then
Me.dgVarData.ScrollBars = ScrollBars.Horizontal
Else
Me.dgVarData.ScrollBars = ScrollBars.None
End If
End Sub

I'm sure a similar method would work for height. I have a max width that I have set for form limitation, the '50' is to prevent the horizontal scrollbar appearing

Hope this helps

kiwicoder2 at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic IDE...