How to get GridView grid to resize on a windows form

brgdotnet
brgdotnet used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chief Technology Ninja
Distinguished Expert 2018
Commented:
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
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
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.
brgdotnetcontractor

Author

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.
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
and you are sure that your panel is properly anchored? put a border to see it it resize.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Please post your code so we can have a look.
Kyle AbrahamsSenior .Net Developer

Commented:
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.
brgdotnetcontractor

Author

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 Technology Ninja
Distinguished Expert 2018

Commented:
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.
brgdotnetcontractor

Author

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 Technology Ninja
Distinguished Expert 2018

Commented:
Please set these properties in the designer. You are doing it in Form Load. And then give it a try.
Kyle AbrahamsSenior .Net Developer

Commented:
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

brgdotnetcontractor

Author

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 Technology Ninja
Distinguished Expert 2018
Commented:
Please have a look at the following screen-shots
PanelGridView
Kyle AbrahamsSenior .Net Developer

Commented:
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.
brgdotnetcontractor

Author

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 Developer

Commented:
try dropbox or something.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial