Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello programmers. I need a Help. I am working on my Project in Windows Forms. In this Project I am getting data from Excel file and than I am displaying this data in datagtidview. I want to autoresize datagridview. For Example when it has 2 rows, dgv height must be equal to 2 rows. For width similar . I have code for that but unfortunately, when data in DVG is too much, it has max height, but it is not Scroling.For width it works perfect but Height is a Problem. Scroling function donn't works. here is a Code:
C#
int height = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                height += row.Height;
            }
            height += dataGridView1.ColumnHeadersHeight;

            int width = 0;
            foreach (DataGridViewColumn col in dataGridView1.Columns)
            {
                width += col.Width;
            }
            width += dataGridView1.RowHeadersWidth;
            dataGridView1.ClientSize = new Size(width + 2, height + 2);
Posted
Comments
PIEBALDconsult 1-Jan-16 21:44pm    
I am working on a similar WinForms app and I have a suitable method that I intend to write a Tip about very soon. Maybe I'll do that this weekend.
The key is that you don't resize the DGV; you resize the Form.
dave_bulac 2-Jan-16 5:02am    
I am resizing a Form and changing it's Position after filling dgv with datas with following code : this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(-10, 0);
this.Height = Screen.PrimaryScreen.WorkingArea.Height;

Ah, fresh off the presses: Resize Form To Fit DataGridView[^]
 
Share this answer
 
What ContainerControl (Form ?, Panel ?) is the DataGridView sited in now ?

Please describe what you are doing now (or not doing) with the DataGridView:

Properties:

ScrollBars, ColumnHeadersHeightSizeMode, RowHeadersWidthSizeMode, AllowUserToResizeRows, AllowUserToReSizeColumns, AutoSizeRowsMode, AutoSizeColumnsMode, Anchor, Dock, MinimumSize, MaximumSize

Events:

Have you tried defining a SizeChanged EventHandler, or a RowHeadersWidthChanged handler, or a ColumnHeadersHeightChanged handler, or a UserAddedRow handler ?
 
Share this answer
 
Comments
dave_bulac 2-Jan-16 5:00am    
I have DGV in Windows Form. So for example I have one From with DGV and one Button. After running it Form size is 300x300 pixels. In that time DGV is exmpty, but after clickinng on Button, I am getting data from DB and display it in DGV. In this OnClick Event Form size changes, it is going on left top corner(this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(-10, 0);) and its height is equal to = (this.Height = Screen.PrimaryScreen.WorkingArea.Height); and now I want to make DGV autoresizable too. Code above for DGV I have found in Internet.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900