1

We have a DataGridView which has 2048 columns. We must provide a way for the user to increase and decrease the width of all columns in the DataGridView.

Currently, we do the following in a button click handler:

for (int i = 0; i < dgv.Columns.Count; i++)
{
   dgv.Columns[i].Width += 5;
}

But that takes a while! (around 2 seconds to be more specific). (Note: We set the ColumnHeadersHeightSizeMode property to DataGridViewColumnHeadersHeightSizeMode.DisableResizing to gain some performance, but that doesn't cut it)

Is there a faster way to achieve the column resizing?

SomethingBetter
  • 1,294
  • 3
  • 16
  • 32
  • 2048 columns? Could you possibly reconsider that ui instead? – David Hall Jul 06 '11 at 14:12
  • I wish I could. I do need 2048 columns (== my customer says he needs it). I'm open to suggestions on other ways to implement the functionality though. Maybe dataGridView isn't the best way to do it. – SomethingBetter Jul 07 '11 at 07:22
  • 1
    have a look at this post: http://stackoverflow.com/questions/587508/suspend-redraw-of-windows-form not sure if it will solve your problem, but the ideas there might help, particularly double buffering. – David Hall Jul 07 '11 at 09:44

1 Answers1

1

You could try attaching a separate event to the button click for each column, something like this:

for (int i = 0; i < 500; i++)
{
    DataGridViewTextBoxColumn c = new DataGridViewTextBoxColumn();
    dataGridView1.Columns.Add(c);

    button1.Click += (o, e) => {                    
        c.Width = 10;                    
    };
}

I tried this on a hunch and it looks like it works. I"m not sure though whether there are any side effects, whether or not there is a better method or even whether it would work for a non-trivial example - all my columns are empty and unbound.

David Hall
  • 32,624
  • 10
  • 90
  • 127
  • That works, but takes the same amount of time as our code. When we click the button, the event handler gets called 2048 times and that takes about the same amount of time. – SomethingBetter Jul 07 '11 at 07:20
  • Moreover, creating the event handler in the column generate loop takes a lot of time, when the grid is first being initialized :( – SomethingBetter Jul 07 '11 at 07:21