Resize DataGridView Columns

I have a simple form with a DataGridView element. In the constructor, grid columns are added and the DataTable is set. When I call AutoResizeColumns (), it does not resize the columns, as it would when called, for example. event buttons. 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 this point. By the way, I need this to implement such behavior as requested / described here . Any ideas?

+5
source share
3 answers

In addition, I believe that the object must be VISIBLE before it resizes ... for some reason, the picture does not look as you would expect.

+7

, :

dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
+11

You need to use it AutoResizeColumns()after you DataGridViewhave data in it. This is the reason. Make sure SomeClass.Tableto get data before callingAutoResizeColumns()

0
source

All Articles