'Center grid in datagridview winforms
I have a datagridview which fills up the entire screen but sometimes the data is too small or resolution too big and the columns don't fill up leaving an ugly blank space like in picture below.
I want the grid to be centered along the gridview.
I cannot use Fill resizing because the data can be massive sometimes and I need a horizontal scroll
Solution 1:[1]
If a MinimumWidth is specified for each column, it will do the trick with AutoSizeColumnsMode set to Fill.
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
column.MinimumWidth = 100;
column.FillWeight = 100;
}
When the displayed columns are smaller than the DataGridView, it will fill the entire view. When it's wider, it will apply a horisontal scroll.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mathias Ovesen |
