Force resize of DataGridView columns

c#, winforms

Solution

Additionally, I believe the object needs to be VISIBLE before the resize is done... for some reason, the painting doesn't appear to happen as one would expect.

Problem

I've got a simple form with a DataGridView element on it. In the constructor the grid columns get added and the DataTable gets set. When I then call AutoResizeColumns() it doesn't resize the columns as it would when called by e.g. a button event. The code looks like this (simplified): ``` public MyDialog() { InitializeComponent(); dgv.Columns.AddRange(SomeClass.MyColumns); dgv.DataSource = SomeClass.Table; // This doesn't work: dgv.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); } ``` AutoResizeColumns() works in general but not at that point. Btw, I need this to implement a behavior like it is requested/described here. Any ideas?

Original source

Related problems