I have a form with a DataGridView
and I want to set the columns AutoSizeMode
to Fill
and the grids ColumnHeadersHeightSizeMode
You issue is related with the fact that main thread draw interface.
Actually you handle drawing (starting position) before application run is launched and before layout operation (controls) are all completly initialize inside suspend layout block.
Changing your approach solve the issue (no need to change your code, only add some pieces).
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyForm());
}
public class MyForm : Form
{
public MyForm()
{
this.InitializeComponents();
}
private void MyForm_Shown(object sender, EventArgs e)
{
this.SetDesktopLocation(Cursor.Position.X - 30, Cursor.Position.Y - 40);
}
private void InitializeComponents()
{
this.SuspendLayout();
this.StartPosition = FormStartPosition.Manual ;
var grid = new DataGridView { Dock = DockStyle.Fill };
grid.Columns.Add("ColumnName", "HeaderText");
// The form will load if I remove one of the two next lines:
grid.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
grid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
Controls.Add(grid);
this.Shown += new System.EventHandler(this.MyForm_Shown);
this.ResumeLayout(false);
}
}
}