Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView Column Widths as Percentage

Is there any way to give each column in a DataGridView a percentage width of the total grid? I am currently using fixed widths but would like to give one column a 15% width, another one a 25% width, and so on so that 100% of the table is filled and resizes with the Grid.

like image 869
Brett Powell Avatar asked Mar 22 '13 20:03

Brett Powell


2 Answers

Try using the DataGridViewColumn.FillWeight property. Basically you assign a weight to every column and the columns re-size according to those weights. The MSDN arcticle is not that great. See the below article for better explaination -

Presenting Data with the DataGridView Control in .NET 2.0—Automatic Column Sizing

like image 137
Aseem Gautam Avatar answered Oct 04 '22 10:10

Aseem Gautam


Try this

    private void DgvGrd_SizeChanged(object sender, EventArgs e)
    {
        dgvGrd.Columns[0].Width = (int)(dgvGrd.Width * 0.2);
        dgvGrd.Columns[1].Width = (int)(dgvGrd.Width * 0.2);
        dgvGrd.Columns[2].Width = (int)(dgvGrd.Width * 0.4);
        dgvGrd.Columns[3].Width = (int)(dgvGrd.Width * 0.2);
        // also may be a good idea to set FILL for the last column
        // to accomodate the round up in conversions
        dgvGrd.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; 
    }
like image 24
Sylvio Avatar answered Oct 04 '22 10:10

Sylvio