56

I have a WinForms application with DataGridView control. My control has five columns (say "Name", "Address", "Phone" etc)

I am not happy with default column width. I want to have more control over column appearance. What I want is to be able to do one of the following:

  • Set width of each column in percent
  • Set width of each column in pixels
  • Use some other best-practive method (make width to fit text etc)

Please suggest - which property to use and how.

Captain Comic
  • 15,744
  • 43
  • 110
  • 148

14 Answers14

72

You can use the DataGridViewColumn.Width property to do it:

DataGridViewColumn column = dataGridView.Columns[0];
column.Width = 60;

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.width.aspx

phoenix
  • 7,988
  • 6
  • 39
  • 45
Bhaskar
  • 10,537
  • 6
  • 53
  • 64
  • 3
    Thanks! I just realized why it was not working for me. MSDN says If the specified value when setting this property is less than the value of the MinimumWidth property, the MinimumWidth property value is used instead. – Captain Comic Jan 28 '10 at 11:47
  • 3
    Yes, width is not available directly to the Grid control, so you use ItemStyle.With to set its property. – Bhaskar Jan 28 '10 at 11:49
  • To add to this, if working in visual studio, right-click, click edit columns and then scroll down. This property (i.e. column.width) will be there. – James Ashwood Oct 31 '21 at 15:06
  • How would you do this if you're using a datasource? I set `AutoSizeMode` to None and then changed the width but I get the error `Property not available for that object.`. – Icoryx Oct 14 '22 at 08:53
44

The following also can be tried:

DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells

or use the other setting options in the DataGridViewAutoSizeColumnsMode Enum

Cleptus
  • 3,446
  • 4
  • 28
  • 34
Mark
  • 441
  • 4
  • 2
28

Most of the above solutions assume that the parent DateGridView has .AutoSizeMode not equal to Fill. If you set the .AutoSizeMode for the grid to be Fill, you need to set the AutoSizeMode for each column to be None if you want to fix a particular column width (and let the other columns Fill). I found a weird MS exception regarding a null object if you change a Column Width and the .AutoSizeMode is not None first.

This works

chart.AutoSizeMode  = DataGridViewAutoSizeColumnMode.Fill;
... add some columns here
chart.Column[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
chart.Column[i].Width = 60;

This throws a null exception regarding some internal object regarding setting border thickness.

chart.AutoSizeMode  = DataGridViewAutoSizeColumnMode.Fill;
... add some columns here
 // chart.Column[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
chart.Column[i].Width = 60;
Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
21

I know this is an old question but no one ever answered the first part, to set width in percent. That can easily be done with FillWeight (MSDN). In case anyone else searching comes across this answer.

You can set DataGridAutoSizeColumnMode to Fill in the designer. By default that gives each column FillWeight of 100. Then in code behind, on FormLoad event or after binding data to grid, you can simply:

gridName.Columns[0].FillWeight = 200;
gridName.Columns[1].FillWeight = 50;

And so on, for whatever proportional weight you want. If you want to do every single column with numbers that add up to 100, for a literal percent width, you can do that too.

It gives a nice full DataGrid where the headers use the whole space, even if the user resizes the window. Looks good on widescreen, 4:3, whatever.

Jeremy L
  • 321
  • 3
  • 7
10

Regarding your final bullet

make width fit the text

You can experiment with the .AutoSizeMode of your DataGridViewColumn, setting it to one of these values:

None
AllCells
AllCellsExceptHeader
DisplayedCells
DisplayedCellsExceptHeader
ColumnHeader
Fill

More info on the MSDN page

hawbsl
  • 15,313
  • 25
  • 73
  • 114
4

i suggest to give a width to all the grid's columns like this :

DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = "phone"            
col.Width = 120;
col.DataPropertyName = (if you use a datasource)
thegrid.Columns.Add(col);    

and for the main(or the longest) column(let's say address) do this :

col = new DataGridViewTextBoxColumn();
col.HeaderText = "address";
col.Width = 120;

tricky part

col.MinimumWidth = 120;
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

tricky part

col.DataPropertyName = (same as above)
thegrid.Columns.Add(col);

In this way, if you stretch the form (and the grid is "dock filled" in his container) the main column, in this case the address column, takes all the space available, but it never goes less than col.MinimumWidth, so it's the only one that is resized.

I use it, when i have a grid and its last column is used for display an image (like icon detail or icon delete..) and it doesn't have the header and it has to be always the smallest one.

kindaska
  • 156
  • 1
  • 6
3
public static void ArrangeGrid(DataGridView Grid)
{ 
    int twidth=0;
    if (Grid.Rows.Count > 0)
    {
        twidth = (Grid.Width * Grid.Columns.Count) / 100;
        for (int i = 0; i < Grid.Columns.Count; i++)
            {
            Grid.Columns[i].Width = twidth;
            }

    }
}
Himanshu
  • 31,810
  • 31
  • 111
  • 133
santosh
  • 377
  • 3
  • 8
2
DataGridView1.Columns.Item("Adress").Width = 60
    DataGridView1.Columns.Item("Phone").Width = 30
    DataGridView1.Columns.Item("Name").Width = 40
    DataGridView1.Columns.Item("Etc.").Width = 30
Schpenn
  • 108
  • 1
  • 11
2

Use:

yourdataView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
jackJoe
  • 11,078
  • 8
  • 49
  • 64
Mahesh
  • 21
  • 1
1

I you dont want to do it programmatically, you can manipulate to the Column width property, which is located inside the Columns property.Once you open the column edit property you can choose which column you want to edit, scroll down to layout section of the bound column properties and change the width.

Nate S.
  • 1,117
  • 15
  • 31
1

You can set default width and height for all Columns and Rows as below only with one loop.

    // DataGridView Name= dgvMachineStatus

   foreach (DataGridViewColumn column in dgvMachineStatus.Columns)
        {
            column.Width = 155;
        }

   foreach (DataGridViewRow row in dgvMachineStatus.Rows)
        {
            row.Height = 45;
        }
Abdul Khaliq
  • 2,139
  • 4
  • 27
  • 31
0

Use the Columns Property and set the Auto Size Mode to All Cells, Resizable to True, Frozen to False and visible to True.

The column will automatically resize based on the data inserted.

Tee Shot
  • 69
  • 1
  • 1
0

simply

dataGridView1.Columns[0].FillWeight = 20;
Mahdhiali
  • 11
  • 1
-3

or Simply you can go to the form and when you call the data to be displayed you set the property like datagridview1.columns(0).width = 150 datagridview1.columns(1).width = 150 datagridview1.columns(2).width = 150enter code here

So simple worked so fine with me Bro