134

I am populating a DataGridView control on a Windows Form (C# 2.0 not WPF).

My goal is to display a grid that neatly fills all available width with cells - i.e. no unused (dark grey) areas down the right and sizes each column appropriately according to the data it contains, but also allows the user to resize any of the columns to their liking.

I am attempting to achieve this by setting the AutoSizeMode of each column to be DataGridViewAutoSizeColumnMode.AllCells except for one of the columns which I set to DataGridViewAutoSizeColumnMode.Fill in order to ensure the entire area of the grid is neatly filled with data. (I don't mind that when the user attempt to resize this column it springs back to a size that ensures the horizontal space is always used.)

However, as I mentioned, once loaded I would like to allow the user to resize the columns to suit their own requirements - in setting these AutoSizeMode values for each column it appears the user is then unable to then resize those columns.

I've tried not setting the AutoSizeMode of all the columns which does allow resizing BUT doesn't set the initial size according to the data the cells contain. The same result occurs when changing the grid's AutoSizeMode back to "Not Set" after loading the data.

Is there a setting I'm missing here which allows automatic setting of default column widths AND user resizing or is there another technique I must use when populating the DataGridView control?

1
  • dont set it to "Not Set" set it to "None" so resizement won't revert Back -- tested for c# , .net2.0
    – bh_earth0
    Commented Sep 29, 2015 at 7:47

25 Answers 25

171

This trick works for me:

grd.DataSource = DT;
    
// Set your desired AutoSize Mode:
grd.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
grd.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
grd.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    
// Now that DataGridView has calculated it's Widths; we can now store each column Width values.
for (int i = 0; i <= grd.Columns.Count - 1; i++)
{
    // Store Auto Sized Widths:
    int colw = grd.Columns[i].Width;
    
    // Remove AutoSizing:
    grd.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    
    // Set Width to calculated AutoSize value:
    grd.Columns[i].Width = colw;
}

In the Code above: You set the Columns AutoSize Property to whatever AutoSizeMode you need. Then (Column by Column) you store each column Width value (from AutoSize value); Disable the AutoSize Property and finally, set the Column Width to the Width value you previously stored.

7
  • 2
    I put similar code in a routine named AutoResizeColumnWidthsYetAllowUserResizing. It's called after the grid is populated initially and also after the user edits data (that is, from the grid's CellEndEdit event). Commented Dec 2, 2010 at 16:34
  • 7
    This is great code. Needs to be put in the 'DataGridView1_DataSourceChanged' event.
    – user890332
    Commented Feb 8, 2012 at 17:58
  • 1
    It seems to me that doing grd.Columns(i).Width = grd.Columns(i).Widthwill already do the trick. See here.
    – Antonio
    Commented Sep 24, 2015 at 18:59
  • 2
    for c# similar but with square brackets dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    – barlop
    Commented Jun 30, 2016 at 2:46
  • 1
    Following this answer and other suggestions from it I reduced the code to this private void AutoSizeGridColumns(DataGridView grd) { //set autosize to fill cell contents for (int i = 0; i <= grd.Columns.Count - 1; i++) { grd.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; grd.Columns[i].Width = grd.Columns[i].Width; //this allows for the width to be user sizable as the AllCells locks it } }
    – kuklei
    Commented Sep 10, 2021 at 11:39
51

Maybe you could call

dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);

After setting datasource. It will set the width and allow resize.

More on MSDN DataGridView.AutoResizeColumns Method (DataGridViewAutoSizeColumnsMode).

10
  • 2
    I'm not sure why this answer isn't getting more attention. Much cleaner. Although if you're looking to match cell content width DataGridViewAutoSizeColumnsMode.AllCells works a bit better. Commented Aug 15, 2012 at 15:06
  • 41
    using this solution i'm getting the next error: "Parameter autoSizeColumnMode is not valid for this operation. It cannot be NotSet, None or Fill but needs to indicate a sizing criteria.". I ended up using this dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
    – itsho
    Commented Oct 25, 2012 at 22:02
  • 6
    Using DataGridViewAutoSizeColumnMode.Fill doesn't work because it ignores the cell contents when sizing columns. Commented Dec 28, 2012 at 23:11
  • 2
    I have used this method with DataGridViewAutoSizeColumnsMode.DisplayedCells. In addition, in the Forms Designer, the AutoSizeColumnsMode is set to None. I needed to execute this method call in the DataBindingComplete event handler of the DataGridView, to make sure it always (re)sizes properly.
    – Daan
    Commented Apr 30, 2014 at 14:32
  • 10
    I dont understand all the upvotes... This does not work at all, the MSDN documentation is clear, doing this leads to an ArgumentException if the autoSizeColumnsMode has the value None or Fill.
    – Larry
    Commented May 23, 2014 at 14:34
34

A C# version of Miroslav Zadravec's code

for (int i = 0; i < dataGridView1.Columns.Count-1; i++)
{
    dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
dataGridView1.Columns[dataGridView1.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
    int colw = dataGridView1.Columns[i].Width;
    dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    dataGridView1.Columns[i].Width = colw;
}

Posted as Community Wiki so as to not mooch off of the reputation of others

23

In my application I have set

grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;

Also, I have set the

grid.AllowUserToOrderColumns = true;
grid.AllowUserToResizeColumns = true;

Now the column widths can be changed and the columns can be rearranged by the user. That works pretty well for me.

Maybe that will work for you.

3
  • Setting the grid's AutoSizeColumnsMode to "Fill" seems to set all columns to the same widths. Yes the columns are then resizable but the initial widths are all wrong. I may need to set the column widths in code "manually". Commented Jun 22, 2009 at 11:06
  • DV- grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumn-> s <- Mode.Fill; (you missed the s, it's ColumsMode on the left hand side and the right hand side, so that line of yours doesn't compile) The code to get datagridview to autosize is very annoying as it is, so at least check your answer first. It's the very first line you wrote and is wrong.
    – barlop
    Commented Jun 2, 2016 at 4:58
  • @barlop thanks for your reply. You have the privileg to edit questions and answers. If you spot a mistake in my code then feel free to edit.
    – Jehof
    Commented Jun 2, 2016 at 5:26
20

After adding the data to the grid add the following code which will adjust the column according to the length of data in each cell

dataGrid1.AutoResizeColumns();            
dataGrid1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

Here is the Result

enter image description here

1
  • 1
    Works like charm
    – Umair
    Commented Sep 3, 2021 at 17:39
12

Well, I did this like this:

dgvReport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
dgvReport.AutoResizeColumns();
dgvReport.AllowUserToResizeColumns = true;
dgvReport.AllowUserToOrderColumns = true;

in that particular order. Columns are resized (extended) AND the user can resize columns afterwards.

11

A simple two lines of code works for me.

dataGridView.DataSource = dataTable;
dataGridView.AutoResizeColumns();
6

If I understood the question correctly there should be an easier way to accomplish what you need. Call dgvSomeDataGrid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

That should do the trick. However, there is one pitfall as you cannot simply call this method directly after populating your DataGridView control. Instead you will have to add an EventHandler for the VisibleChanged event and call the method in there.

1
  • 1
    That would resize columns according to content, but would not ensure all available grid space is used. I.e., does not "fill" remaining space if there is any. Commented Dec 28, 2012 at 23:15
5

Resume of the question:
Have column width adapt to the content (with different method across the column),
but then allow the user to set the column width...

Developing from Miroslav Zadravec's answer, for me what worked was immediately using the auto computed column.Width to set... column.Width!

foreach (DataGridViewColumn column in dataGridView.Columns)
{
    if (/*It's not your special column*/)
    {
        column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        column.Width = column.Width; //This is important, otherwise the following line will nullify your previous command
        column.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
    }
}

//Now do the same using Fill instead of AllCells for your special column

This is tested to work when the DataGridView is already created, using a trick like this.

1
  • I prefer using a foreach as your code does. It makes it more readable when you don't have any math in the top of the loop. I did it that way, and the "column.Width = column.Width;" is interesting.
    – Greg Barth
    Commented Jul 7, 2017 at 16:56
4

This autofits all columns according to their content, fills the remaining empty space by stretching a specified column and prevents the 'jumping' behaviour by setting the last column to fill for any future resizing.

// autosize all columns according to their content
dgv.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
// make column 1 (or whatever) fill the empty space
dgv.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
// remove column 1 autosizing to prevent 'jumping' behaviour
dgv.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
// let the last column fill the empty space when the grid or any column is resized (more natural/expected behaviour) 
dgv.Columns.GetLastColumn(DataGridViewElementStates.None, DataGridViewElementStates.None).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
1
  • I know it's an old answer, but I works nicelly, even when the number of columns is not known in advance.
    – Nilo Paim
    Commented Aug 26, 2016 at 13:51
4

This did wonders for me:

dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
2
  • 1
    Simple solution!
    – Mark Kram
    Commented Jan 19, 2018 at 1:30
  • 1
    Worked for me only if set to None afterwards, i.e. dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
    – Do-do-new
    Commented Jun 22, 2018 at 10:20
2

Slightly neater C# code from Miroslav Zadravec's code assuming all columns are to be autosized

for (int i = 0; i < dgvProblems.Columns.Count; i++)
{
    dgvProblems.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    int colw = dgvProblems.Columns[i].Width;
    dgvProblems.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    dgvProblems.Columns[i].Width = colw;
}
2

dataGridView1.AutoResizeColumns();

2

The column widths set to fit its content I have used the bellow statement, It resolved my issue.

First Step :

RadGridViewName.AutoSize = true;

Second Step :

// This mode  fit in the header text and column data for all visible rows. 
this.grdSpec.MasterTemplate.BestFitColumns();

Third Step :

for (int i = 0; i < grdSpec.Columns.Count; i++) 
{
    // The column width adjusts to fit the contents all cells in the control.
    grdSpec.Columns[i].AutoSizeMode = BestFitColumnMode.AllCells; 
}
2

Another version of Miroslav Zadravec's code, but slightly more automated and universal:

    public Form1()
    {
        InitializeComponent();
        dataGridView1.DataSource = source;
        for (int i = 0; i < dataGridView1.Columns.Count - 1; i++) {
            dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }
        dataGridView1.Columns[dataGridView1.Columns.Count].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

    }

    void Form1Shown(object sender, EventArgs e)
    {
        for ( int i = 0; i < dataGridView1.Columns.Count; i++ )
        {
            int colw = dataGridView1.Columns[i].Width;
            dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            dataGridView1.Columns[i].Width = colw;
        }
    }

I put second part into separate event, because I fill datagridvew in initialization of form and if both parts are there, nothing is changing, because probably autosize calculates widths after datagridview is displayed, so the widths are still default in Form1() method. After finishing this method, autosize does its trick and immediately after that (when form is shown) we can set the widths by second part of the code (here in Form1Shown event). This is working for me like a charm.

2

Here's a simplified code for Miroslav Zadravec's answer in c#:

CurrentDGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;
for (int i = 0; i < dataGridView1.Columns.Count; i++) dataGridView1.Columns[i].Width = dataGridView1.Columns[i].Width;
CurrentDGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
1

Did you try to set up the FillWeight property of your DataGridViewColumns object?

For example:

this.grid1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
this.grid1.Columns[0].FillWeight = 1.5;

I think it should work in your case.

1

A little improvement from Schnapple's version

int nLastColumn = dgv.Columns.Count - 1;
for (int i = 0; i < dgv.Columns.Count; i++)
{
    if (nLastColumn == i)
    {
        dgv.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    }
    else
    {
        dgv.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
    }
}

for (int i = 0; i < dgv.Columns.Count; i++)
{
    int colw = dgv.Columns[i].Width;
    dgv.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    dgv.Columns[i].Width = colw;
}
1
foreach (DataGridViewColumn c in dataGridView.Columns)
    c.Width = c.GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);

This should work whether the dataGridView has been displayed or not (i.e. even if called from the class constructor).

The same method, but with DataGridViewAutoSizeColumnMode.DisplayedCells, fails in the above case for the obvious reason - no cell has been displayed yet! For some non-obvious reason, AutoResizeColumns also fails in this case.

0

If you bind your datasource to a datatable for example, you need to set the properties after the binding is done:

        private void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
            dgv.AutoResizeColumns();
            dgv.AllowUserToResizeColumns = true;
        }
0
0
  • Thanks for the solution above (To iterate through the DataGridView.Columns, change AutoSizeMode to a valid one, collect width value and set it back after change AutoSizeMode to DataGridViewAutoSizeColumnMode.None).
  • I struggled with it, and noticed it won't work whenever it is called from the class constructor or any line before Form.Show() or Form.ShowDialog(). So I put this code snippet in the Form.Shown event and this works for me.
  • My transformed code, reguardless of whatever DataGridView.AutoSizeColumnsMode set before, I use DataGridViewColumn.GetPreferredWidth() instead of changing DataGridViewColumn.AutoSizeMode and set the width value immediately, then change DataGridView.AutoSizeColumnsMode once:

    private void form_Shown(object sender, EventArgs e)
    {
            foreach (DataGridViewColumn c in dataGridView.Columns)
                c.Width = c.GetPreferredWidth(DataGridViewAutoSizeColumnMode.DisplayedCells, true);
            dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
    }
    
  • Be sure to set

            dataGridView.AllowUserToResizeColumns = true;
    
  • I don't know how come this only works after the form is shown.

0

I had to do this in VB and prefer to split it out to a method that I placed in a Module. You can add the Fill column as another ByRef parameter if desired.

''' <summary>
''' Makes all columns in a DataGridView autosize based on displayed cells,
''' while leaving the column widths user-adjustable.
''' </summary>
''' <param name="dgv">A DataGridView to adjust</param>
Friend Sub MakeAdjustableAutoSizedGridCols(ByRef dgv As DataGridView)
    Dim width As Integer

    For Each col As DataGridViewColumn In dgv.Columns
        col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        width = col.Width
        col.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
        col.Width = width
    Next
    dgv.AllowUserToResizeColumns = True
End Sub
0

You could do something like this:

   grd.DataSource = getDataSource();

    if (grd.ColumnCount > 1)
    {
        for (int i = 0; i < grd.ColumnCount-1; i++)
            grd.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

        grd.Columns[grd.ColumnCount-1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    }

    if (grd.ColumnCount==1)
        grd.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

All columns will adapt to the content except the last one will fill the grid.

0

With $array being the contents of a PSCustomObject, this works:

$dataGridView1.DataSource=[collections.arraylist]($array)
$dataGridView1.Columns | Foreach-Object{$_.AutoSizeMode = [System.Windows.Forms.DataGridViewAutoSizeColumnMode]::AllCells}
0

Could do like below

dataGrid1.AutoResizeColumns();

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 18, 2023 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.