473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

datagridview: how to resize the whole grid

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
Dec 26 '07 #1
4 36290
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

Dec 26 '07 #2
On Dec 26, 10:56 pm, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
wrote:
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...
Dec 26 '07 #3
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.

"vbDavidC" wrote:
On Dec 26, 10:56 pm, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
wrote:
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...
Dec 27 '07 #4

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

Feb 5 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Aaron Smith | last post by:
Ok, this is an odd one, but I could use some assistance with the framework 2 in VB.Net... I want to have a DataGridViewColumn, only have it use the ComboBox, then when they drop down the...
1
by: Robert G | last post by:
I'm using VB .net 2003. I have a grid where everything is working fine except when after the grid is filled with records (by setting the data source to a data view), both the horizontal and...
4
by: tasiekk | last post by:
I need to know how i can resize datagrid on a page when user will resize web browser window or maximize this window. Resizeing normal DIV is simple - expression but i dont know how to resize more...
3
by: Prashwee | last post by:
Hello All In my windows application I am using DataGridView as my grid control. I populated let's say 10 rows of data and modified 3 random rows. I need to put some sort of Edit Marker in the...
0
by: lhuisingh | last post by:
I have a data grid in my C# Windows Form. All but the last column can be resized during program execution. If the resized column reaches the right edge of the grid control I can scroll...
0
by: Larry H | last post by:
I have a data grid in my C# Windows Form. All but the last column can be resized during program execution. If the resized column reaches the right edge of the grid control I can scroll...
0
by: AishaKhalfan | last post by:
Hi All, I’m facing a problem while I’m printing. I would to print the whole grid view. Could provide me with a solution, please?. It's Windows Application I’m looking forward to answering my...
0
by: Vincent | last post by:
I came across some code to automatically resize a flex grid. It works adequately--it will resize to allow all of the text to be seen in a given column. But, it resizes the column too much. That...
2
by: gm41970 | last post by:
I have a datagridview in a .NET C# app that I have set to be read-only (grdXXXX.ReadOnly = true) since I do not want users to edit data that is already there. But I need to allow user to insert new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.