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
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;
}
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.
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).
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.
This did wonders for me:
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
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}