简体   繁体   中英

c# resize datagridview columns to fit control

I have a datagridview that is docked and anchored with a panel on a Winform. When I resize the form, the datagridview resizes as expected, but the columns do not resize to fit the datagridview. Instead, I am left with the background colour of the Datagridview.

How can I get the columns to grow with the control?

Thanks.

You could always use the AutoSizeColumnsMode property

This property lets you configure the control so that column widths are automatically adjusted either to fill the control or to fit cell contents. Size adjustments occur in fill mode whenever the width of the control changes.

There's a lot more information on the MSDN page for this.

You can set AutoSizeMode property of one of the columns to be Fill . Then this column will always resize itself to fill all the available space not used by other columns.

private void dataGrid_SizeChanged(object sender, EventArgs e)
{
    ResizeGridColumns();
}

private void ResizeGridColumns()
{
    //get sum of non-resizable columns width
    int diffWidth = 0;
    foreach (DataGridViewColumn col in this.dataGrid.Columns)
    {
        if (col.Resizable == DataGridViewTriState.False && col.Visible) diffWidth += col.Width;
    }

    //calculate available width
    int totalResizableWith = this.dataGrid.Width - diffWidth;

    //resize column width based on previous proportions
    this.dataGrid.ColumnWidthChanged -= new DataGridViewColumnEventHandler(dataGrid_ColumnWidthChanged);
    for (int i = 0; i < this.colWidthRaport.Count; i++)
    {
        try
        {
            if (this.dataGrid.Columns[i].Resizable != DataGridViewTriState.False && this.dataGrid.Columns[i].Visible)
            {
                this.dataGrid.Columns[i].Width = (int)Math.Floor((decimal)totalResizableWith / this.colWidthRaport[i]);
            }
        }
        catch { }
    }
    this.dataGrid.ColumnWidthChanged += new DataGridViewColumnEventHandler(dataGrid_ColumnWidthChanged);
}

private void dataGrid_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
    CalculateGridColWidthsRaport();
}

/// <summary>Calculates the proportions between grid width and column width</summary>
private void CalculateGridColWidthsRaport()
{
    //get sum of non-resizable columns width
    int diffWidth = 0;
    int colWidthsSum = 0;
    foreach (DataGridViewColumn col in this.dataGrid.Columns)
    {
        if (col.Visible)
        {
            colWidthsSum += col.Width;
            if (col.Resizable == DataGridViewTriState.False) diffWidth += col.Width;
        }
    }
    colWidthsSum += 24;

    //calculate available with
    int totalResizableWith = colWidthsSum - diffWidth;// this.dataGrid.Width - diffWidth;
    if (this.ParentForm.WindowState == FormWindowState.Maximized)
    {
        totalResizableWith = this.dataGrid.Width - diffWidth;
    }

    //calculate proportions of each column relative to the available width
    this.colWidthRaport = new List<decimal>();
    foreach (DataGridViewColumn col in this.dataGrid.Columns)
    {
        this.colWidthRaport.Add((decimal)totalResizableWith / (decimal)col.Width);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM