Blablablaster Blablablaster - 1 year ago 57
C# Question

Add thousands of columns into datagridview

I'm trying to add very large number of columns into datagridview and I faced a challenging problem. When column count becomes too large the code below adds columns very slowly. This depends upon

size
value - when it less than 10000 I get more or less good result(2-4 seconds to add this number of columns), but when size grows up to 15000 or more the time of adding is not proportional at all, for 30000 of columns it can reach 2 minutes instead of 20-30 seconds as I expect. So my question is it possible to optimize this procedure somehow?

datagridview.SuspendLayout();
int size = 10000;
var columns = new DataGridViewColumn[size];
for (int i = 0; i < size; i++)
{
columns[i] = new DataGridViewTextBoxColumn();
columns[i].Name = "col" + i;
columns[i].HeaderText = "col" + i;
columns[i].FillWeight = 0.00001f;
}
datagridview.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
Array.ForEach(columns, item => datagridview.Columns.Add(item));
datagridview.ResumeLayout();





And this is what I've done:

int visibleColumns = 20;
string[] headers;
DataGridViewColumn[] columns;
HScrollBar hbar = new HScrollBar();

public Constructor(){
...
int sizeDezired = 15000;
int size = Math.Min(sizeDezired, visibleColumns);
columns = new DataGridViewColumn[size];
headers = new string[sizeDezired];
for (int i = 0; i < size; i++)
{
columns[i] = new DataGridViewTextBoxColumn();
columns[i].Name = "col" + i;
columns[i].HeaderText = "col" + i;
columns[i].FillWeight = 0.00001f;
}
for (int i = 0; i < sizeDezired;i++ )
{
headers[i] = "col" + i;
}
if (sizeDezired > size)
{
hbar.Maximum = sizeDezired - size;
hbar.Minimum = 0;
hbar.Value = 0;
}
hbar.Scroll += hbar_Scroll;
...
}

void hbar_Scroll(object sender, ScrollEventArgs e)
{
for (int i = 0; i < datagridview.ColumnCount; i++)
{
datagridview.Columns[i].HeaderText = headers[i + e.NewValue];
}
}

Answer Source

I added this code:

int visibleColumns = 20;// columns that are in data grid view
string[] headers;// headers for all desired columns 
DataGridViewColumn[] columns;
HScrollBar hbar = new HScrollBar();

public Constructor(){
    ...
    int sizeDesired = 15000;
    int size = Math.Min(sizeDesired, visibleColumns);
    columns = new DataGridViewColumn[size];
    headers = new string[sizeDesired];
    for (int i = 0; i < size; i++)
    {
        columns[i] = new DataGridViewTextBoxColumn();
        columns[i].Name = "col" + i;
        columns[i].HeaderText = "col" + i;
        columns[i].FillWeight = 0.00001f;
    }
    for (int i = 0; i < sizeDesired;i++ )
    {
        headers[i] = "col" + i;
    }
    if (sizeDesired > size)
    {
        hbar.Maximum = sizeDesired - size;
        hbar.Minimum = 0;
        hbar.Value = 0;
    }
    hbar.Scroll += hbar_Scroll;
    ...
}

void hbar_Scroll(object sender, ScrollEventArgs e)
{
    for (int i = 0; i < datagridview.ColumnCount; i++)
    {
        datagridview.Columns[i].HeaderText = headers[i + e.NewValue];
    }
}

Here horizontal scroll bar added to cycle through all invisible columns and shift column headers to visually "scroll" through all columns(15000 in this example) but in reality only 20 columns are present. This code does not use any databinding, only headers are changing so you need to modify hbar_Scroll handler to show relevant data in cells.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download