开发者

Changing the width of all columns in a DataGridView

开发者 https://www.devze.com 2023-03-17 12:30 出处:网络
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.

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号