Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello friends,

I have written the folowing code to populate the data from a object in a tablelayoutpanel control. It works :), but when its loading the data onto the table, it flickers/jumps for few seconds and then after 2-3 seconds when its done processing the data it populates the data :( . I believe this behaviour is because of the code written for dynamically processing & drawing of the various controls in the table based on the object data.

I need your help in optimising the code/improving the performance of this code so that the table can load smoothly and fast. Please help. Thanks.

PS: This code is written for a table containing small amount of data. But going forward the same is planned for populating table with 4X more data. If this is the case, then performance will be very poor, which worries me. Please suggest some ideas.

C#
private void button1_Click(object sender, EventArgs e)
        {
            Common obj = new Common();
            obj.CreateDeserializedXmlObject(@"E:\TestReport.xml");
            var v = obj.GetAdminData();

            tableLayoutPanel1.ColumnCount = 4;
            tableLayoutPanel1.RowCount = ((v.DOCREVISIONS.Length * 4) + 1 + (v.USEDLANGUAGES.L10.Length));


            Label labelLanguage = new Label();
            Label labelUsedLanguage = new Label();
            Label labelDocRevisions = new Label();

            labelLanguage.Text = "Language:";
            labelUsedLanguage.Text = "Used Language:";
            labelDocRevisions.Text = "Doc-Revisions:";

            ComboBox comboBoxLanguage = new ComboBox();
            comboBoxLanguage.Items.Add(v.LANGUAGE.Value.ToString());
            comboBoxLanguage.SelectedIndex = 0;

            ComboBox comboBoxUsedLanguage = new ComboBox();
            foreach (LPLAINTEXT Lang in v.USEDLANGUAGES.L10)
            {
                comboBoxUsedLanguage.Items.Add(Lang.L.ToString());
            }
            comboBoxUsedLanguage.SelectedIndex = 0;
            int index = 0;

            Label[] labelDocRevision = new Label[v.DOCREVISIONS.Length];
            Label[] labelRevision = new Label[v.DOCREVISIONS.Length];
            Label[] labelState = new Label[v.DOCREVISIONS.Length];
            Label[] labelTeamMember = new Label[v.DOCREVISIONS.Length];
            Label[] labelDate = new Label[v.DOCREVISIONS.Length];

            TextBox[] textBoxRevision = new TextBox[v.DOCREVISIONS.Length];
            TextBox[] textBoxState = new TextBox[v.DOCREVISIONS.Length];
            TextBox[] textBoxTeamMember = new TextBox[v.DOCREVISIONS.Length]; 
            TextBox[] textBoxDate = new TextBox[v.DOCREVISIONS.Length];

            foreach (DOCREVISION dcr in v.DOCREVISIONS)
            {
                labelDocRevision[index] = new Label();
                labelRevision[index] = new Label();
                labelState[index] = new Label();
                labelTeamMember[index] = new Label();
                labelDate[index] = new Label();

                textBoxRevision[index] = new TextBox();
                textBoxState[index] = new TextBox();
                textBoxTeamMember[index] = new TextBox();
                textBoxDate[index] = new TextBox();

                labelDocRevision[index].Text = "DOCREVISION["+index.ToString()+"]:";
                labelRevision[index].Text = "Revision:";
                labelState[index].Text = "State:";
                labelTeamMember[index].Text = "TeamMemberRef:";
                labelDate[index].Text = "Date:";

                textBoxRevision[index].Text = dcr.REVISIONLABEL.Value.ToString();
                textBoxState[index].Text = dcr.STATE.Value.ToString();
                textBoxTeamMember[index].Text = dcr.TEAMMEMBERREF.Value.ToString();
                textBoxDate[index].Text = dcr.DATE.Value.ToString();
                
                index++;
            }
            
            
            // Add child controls to TableLayoutPanel and specify rows and column
            tableLayoutPanel1.Controls.Add(labelLanguage, 0, 0);
            tableLayoutPanel1.Controls.Add(labelUsedLanguage, 0, 1);
            tableLayoutPanel1.Controls.Add(labelDocRevisions, 0, 2);
            tableLayoutPanel1.Controls.Add(comboBoxLanguage, 1, 0);
            tableLayoutPanel1.Controls.Add(comboBoxUsedLanguage, 1, 1);

            int docRevRowSpacing = 2;
            for (int loop = 0; loop < index; loop++)
            {
                tableLayoutPanel1.Controls.Add(labelDocRevision[loop], 1, docRevRowSpacing);
                tableLayoutPanel1.Controls.Add(labelRevision[loop], 2, docRevRowSpacing);
                tableLayoutPanel1.Controls.Add(labelState[loop], 2, docRevRowSpacing+1);
                tableLayoutPanel1.Controls.Add(labelTeamMember[loop], 2, docRevRowSpacing+2);
                tableLayoutPanel1.Controls.Add(labelDate[loop], 2, docRevRowSpacing+3);

                tableLayoutPanel1.Controls.Add(textBoxRevision[loop], 3, docRevRowSpacing);
                tableLayoutPanel1.Controls.Add(textBoxState[loop], 3, docRevRowSpacing+1);
                tableLayoutPanel1.Controls.Add(textBoxTeamMember[loop],3 , docRevRowSpacing+2);
                tableLayoutPanel1.Controls.Add(textBoxDate[loop], 3, docRevRowSpacing+3);

                docRevRowSpacing += 4;
            }

            tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            Controls.Add(this.tableLayoutPanel1);
        }
Posted
Updated 8-Nov-17 8:57am
v3

1 solution

Try putting the code that generates the new controls between a SuspendLayout/ResumeLayout pair:

C#
private void button1_Click(object sender, EventArgs e)
{
    SuspendLayout();

    // code that recreates controls on the form

    ResumeLayout();
}


This will keep the window from trying to lay out/redraw the controls while they are still being created.

You might also consider using a data grid control; that way, you don't have to generate the controls yourself.
 
Share this answer
 
Comments
[no name] 9-Jun-15 5:08am    
@Staffan: Thanks for the suggestion. I tried that, but unfortunately no improvement.
While doing some trials, i found that - changing the CellBorderStyle from Single to None, speeds up. Alternatively, i tried hiding the tablelayout before processing the data and showing it after everything is done, this also helps.

DataGridView Control was my initial choice, but i faced lot of challenges in binding the object (source) with the Datagrid, also it was difficult adding different controls in required layout, in the datagrid. So i switched to TableLayout.

If anyone can suggest some optimisation for my code, then that will be helpful.
Thank u :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900