How to get GridView grid to resize on a windows form

I have a windows form with a GridView in it. When the form is resized, I want the Grid to resize as well . I tried setting the Anchor attribute for the GridView object but still the GridView will not resize. Note that the GridView is added to a panel control.

Can someone tell me how I can get the Grid to resize when the form resizes?



dgvMyForm As DataGridView

dgvMyForm .Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top Or AnchorStyles.Bottom

Panel1 As System.Windows.Forms.SplitContainer

Panel1.Controls.Add(Me.dgvMyForm)
Form1.txt
Form1.Designer.txt
Form1.resx
WindowsApplication1.vbproj
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
You have to set Docking on GridView as well as the Panel. Set DockStyle to Fill for both the panel and the gridview.
For details, check out: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.dockstyle?redirectedfrom=MSDN&view=netframework-4.7.2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
Chinmay is surely right. Setting the Anchor property will force a control to resize when its parent gets resized. Since the parent of the grid is the panel, you also need to ensure that the panel is correctly anchored.
brgdotnetcontractorAuthor Commented:
Thanks Guys. Unfortunately it does not work still. Any ideas?

So let me explain again. If the form (Which contain both the panel control and Grid control ) is resized, I need the Grid to resize as well.

The only other option I can think of is to call the resize event on the form and then somehow calculate the new size of the grid, and panel control. That can be alot of work I imagine, so this would not be my first preference.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Éric MoreauSenior .Net ConsultantCommented:
and you are sure that your panel is properly anchored? put a border to see it it resize.
Chinmay PatelChief Technical NinjaCommented:
Please post your code so we can have a look.
Kyle AbrahamsSenior .Net DeveloperCommented:
so Anchor will grow in relation to the parent, all the way up.

Is panel 1 directly attached to the form?

What's the size of the panel and form at creation?

EG:  As the parent increases, so should the child if it's anchored properly.

But note that a grandchild will only grow as the parent grows.  So every control from the form down to the dgv in question must have anchoring set on the sides you want it to grow.
brgdotnetcontractorAuthor Commented:
Patel, attached is the code Sir.

I was not able to upload the project as a Zip fle. The VB.net files I had to rename as text files. Pretty small example, but perfect for what I am talking about.


In the designer the panel sits on the form. The Data grid sits inside of the panel control.
If anyone can suggest a way to upload a zip file let me know.
Chinmay PatelChief Technical NinjaCommented:
Hi brgdotnet,

I did these 2 changes
1. The panel, it's a good idea to dock it to either top / or bottom or somewhere else
2. The GridView dock style is set to fill.

Here is a result
Initial Size
Full screen
Regards,
Chinmay.
brgdotnetcontractorAuthor Commented:
Hi Chinmay,

I docked the GridView as you instructed. Still the GridView is not resizing?? Can you look at my code below"


  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim table As New DataTable

        ' Create four typed columns in the DataTable.
        table.Columns.Add("Members", GetType(Integer))
        table.Columns.Add("Team", GetType(String))
        table.Columns.Add("Player", GetType(String))
        table.Columns.Add("Date", GetType(DateTime))

        ' Add five rows with those columns filled in the DataTable.
        table.Rows.Add(25, "Rams", "David", DateTime.Now)
        table.Rows.Add(50, "Vikings", "Sam", DateTime.Now)
        table.Rows.Add(10, "Sharks", "Christoff", DateTime.Now)
        table.Rows.Add(21, "Sea Hawks", "Jimmy", DateTime.Now)
        table.Rows.Add(100, "Chargers", "Melton", DateTime.Now)

        DataGridView1.Dock = System.Windows.Forms.DockStyle.Fill
        '  Panel1.Dock = System.Windows.Forms.DockStyle.Fill
        DataGridView1.DataSource = table



        '      DataGridView1.Dock = System.Windows.Forms.DockStyle.Fill   '.DockStyle.Fill
        'Panel1.Dock = DockStyle.Fill ' Don't set this for the panel, because it makes my button go away and the entire form turns grey.
    End Sub
End Class


'
Chinmay PatelChief Technical NinjaCommented:
Please set these properties in the designer. You are doing it in Form Load. And then give it a try.
Kyle AbrahamsSenior .Net DeveloperCommented:
Actually the gridview IS resizing.  It's the columns / rows that aren't filling the gridview.

try adding:

DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCellsExceptHeaders;
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
DataGridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders);

Open in new window

brgdotnetcontractorAuthor Commented:
Thanks Guys.
Chinmael, there is not dock style property for the panel of grid view in the designer. I only see "Dock" with options of Top, Bottom, None, left and right.

Kyle when the code hits :
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders

The form is displayed, but an exception is thrown. This is really frustrating. If anyone has any more ideas, I would deeply appreciate it. Thanks.
Chinmay PatelChief Technical NinjaCommented:
Please have a look at the following screen-shots
PanelGridView
Kyle AbrahamsSenior .Net DeveloperCommented:
Try one of the other cell formatting styles (EG: All Cells) and see if that works?  Essentially at this point it's a column sizing / row sizing issue.  The datagrid is the dark grey section of the form.  That IS expanding.  Just the cells aren't following the expansion.
brgdotnetcontractorAuthor Commented:
Thanks Guys. I still can't get it to work, where the size of the grid resizes(Grows/Shrinks) when the form is resized. I followed all of the advice given. Does anyone have any creative ideas on how I can upload my entire project to customer exchange? Or is there some other way I can make it available to an expert?
Kyle AbrahamsSenior .Net DeveloperCommented:
try dropbox or something.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.