datagridview: how to resize the whole grid

V

vbDavidC

I have an unbound grid that is on my form (not created
programmatically). I stretched the size to what I think is wide
enough to accomodate the 6 columns I will be creating. I plan on
making the width wide enough so that you don't have to scroll to the
right. I want to know if there is a property to resize the width
programmatically in case I add or remove columns.

thanks
 
C

Cor Ligthert[MVP]

David,

You can easily set the dock property to fill, however, with that you don't
stretch the columns, the keep in a grayed container.

Cor
 
V

vbDavidC

David,

You can easily set the dock property to fill, however, with that you don't
stretch the columns, the keep in a grayed container.

Cor

thanks for the info. However, I am not sure this addresses my
problem. Or maybe I maybe I may not know enough about programming the
DGV properly.

I tried the fill however since the grid is on the form it filled the
form with the data grid. I would like to grid (the gray part) to
'shrink to fit' the columns.

I could set the size of the width manually which would be an easy
solution but I thought there was a way to do it programmitically and
also in case I add/delete columns.

With DataGridView1
.Columns(0).Name = "Firstname"
.AutoResizeColumns()
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.Dock = DockStyle.Fill <-- I added this after your post.
End With

thanks, btw I notice you are very busy helping people out all over the
place...
 
A

AMercer

I understand your objective, and I don't have a direct answer. It sounds
like you need to add up your calculated column widths and make the DGV width
that total, and then maybe horizontally center the DGV on the form. I don't
find this approach attractive.

Would you be interested instead in a DGV that automatically sizes the column
widths based on the current conditions? If so, you might get some use from
DGV.AutoResizeColumns().

I use it as follows. Set DGV's Dock=Fill, AutoSizeColumnsMode=Fill, and
ScrollBars=Both. (Only the vertical scroll bar will be used, and I think
ScrollBars=Vertical works ok too). In Form_Layout, call
DGV.AutoResizeColumns. Now, when the form is resized, DGV columns will also
resize so that (1) DGV remains docked, and (2) DGV column widths are
proportional to the horizontal space they need. A column with single digit
integers will be narrow and a column of 10 character strings will be wide.
When the form is widened/narrowed, both columns will be proportionally
widened/narrowed.

In my case, DGV is readonly, so I need to AutoResizeColumns only in the
Layout event to handle form resize and other issues. You may need additional
calls when columns are added, removed, and possibly other places as well.

I like this approach because it is responsive to user resizing actions. If
he wants it wider, he makes it wider, and DGV widens in an 'intelligent' way.
 
J

Jeff C

Private Sub DataGridView1_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Resize
With DataGridView1
.Columns(0).Width = Me.Width / 2
.Columns(1).Width = Me.Width / 2
End With
End Sub
 

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