Search code examples
c#winformsdatagridviewdatagridviewcolumn

How to reduce DataGridView setting speed


I have more than 365 columns in dataGridView. And I set each of their Width, HeaderCell Value, ReadOnly mode, Header Text. Also, I have ColumnAdded method to set FillWeight as 10.

Setting these values makes my application too slow. dataGridView columns take almost 2 minutes.

Is theres any way to fix this problem?

I've tried setting each columns in Form1.cs[Design] and that made program faster. But I couldn't find way to do it programmatically.

for (int j = 0; j < year; j++)
{
    for (int i = 1; i <= 365; i++)
    {
        dataGridView1.Columns[365 * j + i].Width = 45;
        dataGridView1.Columns[365 * j + i].HeaderCell.Value = i.ToString();
        dataGridView1.Columns[365 * j + i].ReadOnly = true;
        dataGridView1.Columns[355 * j + i].HeaderText = new DateTime().AddDays(Double.Parse(i.ToString())).ToString("MM/dd");
    }
}

dataGridView1.Columns[0].Frozen = true;
dataGridView1.Columns[0].Width = 55;

I'm expecting this program to be finished in less than one minute.


Solution

  • Hopefully this helps,In the DataGridView properties window you have an option called AutoSizeColumnsMode, i usually set that to "AllCells". If you need to to it afterwards for some reason you can do something like:

    dgridSystem.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

    Rows would be similar
    
    
    dgridSystem.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;