Windows Develop Bookmark and Share   
 index > Windows Forms General > c# winform set datagridview coilumn header in code very lengthy
 

c# winform set datagridview coilumn header in code very lengthy

c# winform Hello - I need to set the column headers of 2 datagridviews in code. without settingthem - my app takes 12 seconds to load. With setting them- 26 seconds. I find that unacceptable so maybe you know of another implementation. Mine is
(for the sake of simplicity - I set here the headercell as a running number - but the time consumed is just the same...: 14 seconds!!!)
int cn = 0;
string cName = string.Empty;
while (cDgv < dgv.Columns.Count)
{
cn++;
cName = cn.ToString();
dgvHeader.Columns[cDgvH].HeaderText = cName;
dgv.Columns[cDgv].HeaderText = cName;
cDgvH++;
cDgv++;
}
dgv and dgvHeader are my 2 datagridview
each dgv has 380 columns
the code is called within the Load function
Thanks in advance
Gil Yoktan
Gil Yoktan  Tuesday, September 15, 2009 6:00 AM

Hi Gil Yoktan,

The cause of the low performance is that too many columns are added in the DataGridView. From my experience, we can set the header text of the columns in the constructor of the form which would be faster than in the Load event handler. If we need to pass the header text to the form, we can create a new constructor which takes a string list as the parameter. This is a code snippet:
public Form1(List<string> colHeaderText)

{

InitializeComponent();

const int count = 360;

//Add the columns.

for (int i = 0; i < count; i++)

{

this.dataGridView1.Columns.Add(i.ToString(),i.ToString());

}

//Set the header text of the columns.

for (int i = 0; i < count; i++)

{

this.dataGridView1.Columns[i].HeaderText = colHeaderText[i];

}

}

Let me know if this does not help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 16, 2009 10:18 AM

Hi Gil Yoktan,

The cause of the low performance is that too many columns are added in the DataGridView. From my experience, we can set the header text of the columns in the constructor of the form which would be faster than in the Load event handler. If we need to pass the header text to the form, we can create a new constructor which takes a string list as the parameter. This is a code snippet:
public Form1(List<string> colHeaderText)

{

InitializeComponent();

const int count = 360;

//Add the columns.

for (int i = 0; i < count; i++)

{

this.dataGridView1.Columns.Add(i.ToString(),i.ToString());

}

//Set the header text of the columns.

for (int i = 0; i < count; i++)

{

this.dataGridView1.Columns[i].HeaderText = colHeaderText[i];

}

}

Let me know if this does not help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 16, 2009 10:18 AM
Thanks Aland
I'll try it and let you know
Have a nice day
Gil Yoktan
Gil Yoktan  Monday, September 21, 2009 8:33 AM

You can use google to search for other answers

Custom Search

More Threads

• Notifyicon not responding in c# Windows Service
• To set color in scroll bar
• File verification
• Enabling AxMSFlexGrid to multilingual
• VB.net combobox question
• COM object that has been separated from its underlying RCW cannot be used.
• Command Object Problem (ExecuteScalar)
• WinForms App Draining Memory Resorces
• Metafiles, Clipboard, Drag and Drop support
• question on how to configure reportviewer control