How do you automatically resize columns in a DataGridView control AND allow the user to resize the columns on that same grid?

后端 未结 24 2955
孤城傲影
孤城傲影 2020-11-29 18:03

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 un

相关标签:
24条回答
  • 2020-11-29 18:29

    dataGridView1.AutoResizeColumns();

    0 讨论(0)
  • 2020-11-29 18:31

    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.

    0 讨论(0)
  • 2020-11-29 18:32

    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.

    0 讨论(0)
  • 2020-11-29 18:32

    A simple two lines of code works for me.

    dataGridView.DataSource = dataTable;
    dataGridView.AutoResizeColumns();
    
    0 讨论(0)
  • 2020-11-29 18:35

    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 讨论(0)
  • 2020-11-29 18:36

    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.

    0 讨论(0)
提交回复
热议问题