How to set Grid View column width?

Asked

Viewed 8,783 times

4

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

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

  • Winforms or Webforms?

  • using Winforms...

  • You want to fix the size of the first column and make the others have the automatic size, but all of them with the same size (except for the first one, of course). Right?

  • That’s right @jbueno

1 answer

2


Via design

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

After that you just need to change the property AutoSizeMode of all other columns to Fill.

DataGridView

Via code

You said you create the columns dynamically, which makes it impossible for you to do what I said above. The solution would be to iterate the columns of the DataGridView and set the sizes after the grid is populated.

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

    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
  • Thank you so much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.