开发者

C# Auto Resize Form to DataGridView's size

I have a Form and a DataGridView. I populate t开发者_Python百科he DataGridView at runtime, so I want to know how do I resize the Form dynamically according to the size of the DataGridView? Is there any sort of property or method? Or do I have to determine the size myself and update accordingly?


You can find the actual width by count Columns width.

Don't forget that your form may be more complex and your should count other controls.

public class YourForm : Form
{
    public YourForm()
    {
        DataGridView _dgv = new DataGridView() { Dock = DockStyle.Fill};
        Controls.Add(_dgv);
    }
    public void CorrectWindowSize()
    {
        int width = WinObjFunctions.CountGridWidth(_dgv);
        ClientSize = new Size(width, ClientSize.Height);
    }
    DataGridView _dgv;
}

public static class WinObjFunctions
{
    public static int CountGridWidth(DataGridView dgv)
    {
        int width = 0;
        foreach (DataGridViewColumn column in dgv.Columns)
            if (column.Visible == true)
                width += column.Width;
        return width += 20;
    }
}


int dgv_width = dataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);
int dgv_height = dataGridView1.Rows.GetRowsHeight(DataGridViewElementStates.Visible);
this.Width = dgv_width;
this.Height = dgv_height;

this.Width resizes this Form width.

Of course you've to add fixed values (for example margin, Form title heigth, ecc.). With tests i've reached the values working for me (don't ask why...):

this.Width = dgv_width + 147;
this.Height = dgv_height + 47;


Normally controls adapt their sizes to the size of the containing form. To adjust the size of your form to the size of your DataGridView, you do have to determine the size yourself and then set the form's size to match, remembering to take into account the extra size required by the form's menu strip and/or toolbars, status bars or other controls.

In your case, it would probably be best not to resize the form to match the grid view control. Most likely, you will have many more rows in your grid view than could fit on your Windows screen, and you don't want to have a form that extends below the viewable desktop area. Generally speaking, this type of situation is exactly why you want to have a scrollable grid view - for viewing more data than can fit on the screen at one time.


I would go the other direction and size the grid to the form. (some users may have low res) Set 'WindowState' property of the form => maximized. (optional) Set 'anchor' property of the DGV => 'Top, Bottom, Left, Right'.


You may be able to make use of the PreferredSize property (MSDN PreferredSize entry). For DataGridView controls, I found that the preferred width and height were about 20 units bigger than I expected. I guess that the control might be calculating its preferred size taking scroll bars into account.

Another caveat I discovered is that the PreferredSize calculation will not be accurate immediately after adding or changing items in the table. To get around this I made a handler for the RowHeadersWidthChanged event.

Here is what worked for me:

class GridToy {
    private DataGridView grid;
    public GridToy(DataGridView dgv) {
        grid = dgv;
        grid.RowHeadersWidthChanged += AdjustWidth; // Event handler.
        Layout();
    }

    public void Layout() {
        // Just do some arbitrary manipulation of the grid.
        grid.TopLeftHeaderCell.Value = "Some Arbitrary Title";
    }

    public void AdjustWidth() {
        Control horizontal = grid.Controls[0]; // Horizontal scroll bar.
        Control vertical = grid.Controls[1]; // Vertical scroll bar.
        grid.Width = grid.PreferredSize.Width - vertical.Width + 1;
        grid.Height = grid.PreferredSize.Height - horizontal.Height + 1;
    }
}


You could set the Property " Height " to Auto in the form after classifying or using an ID and this should do it ,,

I just tried it .. and It worked

#form1{
 background-color:white;
 height:auto;                             
 width:1500px;              
 border-top:medium solid #3399FF;
 margin-top:100px;
 margin-left:30px;
 display: inline-block;
 text-align: center;                
 float: none;      
}

I'm just putting exactly what I did in case you got lost .. Don't worry about the other properties there for my design.


Set AutoSizeColumnsMode :Fill in Grid Properties

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜