locked
DataGridView Autosize Row/Column RRS feed

  • Question

  • Is there a way to programatically replicate what happens when you double click on the seperator bar of a row/column header to resize it to fit the contents of the column/row? I have multi line rows and by default the grid does not resize the row to fit the contents
    Wednesday, December 6, 2006 9:54 PM

Answers

  • Hi Nizmo:

    as I know for rows we can use code as follow:
    dataGridView1.AutoResizeRow(2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);

    and for column we can use
    dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

    hope it can help you

    Friday, January 19, 2007 1:17 AM

All replies

  • Anyone?
    Friday, January 19, 2007 12:21 AM
  • Hi Nizmo:

    as I know for rows we can use code as follow:
    dataGridView1.AutoResizeRow(2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);

    and for column we can use
    dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

    hope it can help you

    Friday, January 19, 2007 1:17 AM
  • *shakes head*

    I could have sworn that command wasnt there last time i checked... I can't beleive i could have missed it...

    Friday, January 19, 2007 1:35 AM
  •  

    I am trying to AutoSize the columns, and also allow the user to be able to resize them.  However when I try the above method or even just set the dataGrid AutoSizeColumnsMode to AllCells, then the users is no longer allowed to resize the columns.  Very frustrating.  Does anyone know how to AutoResize while maintaing user resizing permission.

     

    Thanks.

    Monday, August 4, 2008 4:17 PM
  • This answer might be 3 years late, but for anyone seeking a solution, I came up with the following hack:

    To AutoSize columns w/o removing column sizing capability to user, for example:

     

    dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; // set auto-size mode
    
    int initialAutoSizeWidth = dataGridView1.Columns[0].Width; // save calculated width after auto-sizing
    
    dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet; // revert sizing mode to default
    
    dataGridView1.Columns[0].Width = initialAutoSizeWidth; // set width to calculated auto-size
    
    

     

    ...it's a "hack" for me because it doesn't seem clean, but it works. Hope it helps someone.

     

    Find more info at:

    Resizing Columns and Rows in the Windows Forms DataGridView Control

     

    Tuesday, January 18, 2011 10:58 PM
  • again one year later...

    for every one looking for this the hack above isn't actually needed

            For Each col As DataGridViewColumn In DataGridView1.Columns
                 col.Width = col.GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCellsExceptHeader, False)
            Next
    does the same...
    Tuesday, February 28, 2012 4:15 PM
  • yeah is never late...

    your solution can be combined with fill property to make an Filled datagridview with optimum column width:

    datagrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
    foreach (DataGridViewColumn col in datagrid.Columns)
    	col.Width = col.GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, false);

    in this case i used AllCells AutosizeColumnMode to get the preferred width but it can be changed.

    Tuesday, August 21, 2012 7:55 PM
  • again some months later maybe someone looking for solution :)

    i think the original problem is how to resize columns and rows after binding in a data gridview, for me i just did this solution:

    this.dataGridView.DataSource = dataSource;
     
    this.dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

    this.dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

    TAKECARE IF YOU USE:

    this.dataGridView.AutoSizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

    this.dataGridView.AutoSizeRows(DataGridViewAutoSizeRowsMode.AllCells);

    THIS WILL NOT WORK!!!

    Tuesday, February 26, 2013 9:02 AM
  •         int multilineht = 0;
            private void CustGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                //TextBox TB = (TextBox)e.Control;
                //TB.Multiline = true;
                multilineht = CustGridView.Rows[CustGridView.CurrentCell.RowIndex].Height;
                CustGridView.AutoResizeRow(CustGridView.CurrentCell.RowIndex, DataGridViewAutoSizeRowMode.AllCells);
            }

            private void CustGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                CustGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
                CustGridView.Rows[CustGridView.CurrentCell.RowIndex].Height = multilineht;
                CustGridView.Refresh();
            }
    Tuesday, June 26, 2018 6:41 AM
  •         int multilineht = 0;
            private void CustGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                multilineht = CustGridView.Rows[CustGridView.CurrentCell.RowIndex].Height;
                CustGridView.AutoResizeRow(CustGridView.CurrentCell.RowIndex, DataGridViewAutoSizeRowMode.AllCells);
            }

            private void CustGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                CustGridView.Rows[CustGridView.CurrentCell.RowIndex].Height = multilineht;
            }
    Tuesday, June 26, 2018 6:43 AM