How to set column width of Grid View?

4

I would like to know how to set the width of columns of DataGridView manually.

My first column must have a fixed value and the other columns must "fill" the grid, so that they are the same size.

    
asked by anonymous 25.11.2015 / 12:20

1 answer

2

Via design

First click on the little box in the upper right corner of DataGridView and click Edit Columns . This will open a form with all the columns of your Grid, so you select in the column that wants to leave the fixed size ( TESTE1 in that case) and sets the value of the Width property on the right side of the screen.

Then you only need to change the AutoSizeMode property of all other columns to Fill .

Viacode

Yousaidyoucreatethecolumnsdynamically,whichmakesitimpossibleforyoutodowhatIsaidabove.ThesolutionwouldbetoiteratethroughtheDataGridViewcolumnsandsetthesizesafterthegridispopulated.

foreach(DataGridViewColumncolumnindgNotas.Columns){if(column.DataPropertyName=="primeiraColuna")
        column.Width = 100; //tamanho fixo da primeira coluna

    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
    
25.11.2015 / 12:33