Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertically auto size a winforms datagridview control, so that its rows are always visible

I have a DataGridView that displays a limited number of rows, never more than 5. This DataGridViewis placed on a DataRepeater control so it's usually displayed many times on the screen. What I want to achieve is that all grids are resized to the size of their contents so they don't display scroll bars if 4 or 5 items are in them or take up extra vertical space if only 1 or 2 items are there.

The grids only contain text data. They are data bound controls, so they'll need to resize if the underlying data source changes (I guess the DataBindingComplete event would be suitable).

How may I achieve this? Is counting rows the best option? Thanks in advance.

like image 783
Daniel Avatar asked Jul 26 '12 12:07

Daniel


People also ask

How do I resize a DataGridView control when resized?

Users can make size adjustments by dragging or double-clicking row, column, or header dividers. In column fill mode, column widths change when the control width changes; for example, when the control is docked to its parent form and the user resizes the form.

How set DataGridView size in C#?

Set the DataGridView. AutoSizeColumnsMode property to Fill to set the sizing mode for all columns that do not override this value. Set the FillWeight properties of the columns to values that are proportional to their average content widths.

What is difference between DataGridView and DataGrid control in Winforms?

The DataGrid control is limited to displaying data from an external data source. The DataGridView control, however, can display unbound data stored in the control, data from a bound data source, or bound and unbound data together.

How many rows can DataGridView handle?

Max value, i.e. 2,147,483,647. The DataGridView's RowCount cannot hold a number more than this because it's an integer.


2 Answers

Since your control is data-bound, I would set the Height property on the DataGridView to the sum of the heights of its rows (plus some margin) in the DataBindingComplete event:

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    var height = 40;
    foreach (DataGridViewRow dr in dataGridView1.Rows) {
        height += dr.Height;
    }

    dataGridView1.Height = height;
}
like image 74
hmqcnoesy Avatar answered Sep 20 '22 08:09

hmqcnoesy


I took hmqcnoesy's answer and expanded on it and created a function to also include the width. And to use on any grid.

Note: Set AutoSizeCells = AllCells on the grid.

    public static DataGridView SetGridHeightWidth(DataGridView grd, int maxHeight, int maxWidth)
    {
        var height = 40;
        foreach (DataGridViewRow row in grd.Rows)
        {
            if(row.Visible)
                height += row.Height;
        }

        if (height > maxHeight)
            height = maxHeight;

        grd.Height = height;

        var width = 60;
        foreach (DataGridViewColumn col in grd.Columns)
        {
            if (col.Visible)
                width += col.Width;
        }

        if (width > maxWidth)
            width = maxWidth;

        grd.Width = width;

        return grd;
    }
like image 33
Zath Avatar answered Sep 22 '22 08:09

Zath