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

后端 未结 24 2954
孤城傲影
孤城傲影 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:22

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

    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.

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

    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).

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

    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.

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

    This did wonders for me:

    dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
    
    0 讨论(0)
  • 2020-11-29 18:28

    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 讨论(0)
提交回复
热议问题