C# - 如何在TableLayoutPanel中显示对象数据。代码优化性能改进 [英] C# - How to display Object data in a TableLayoutPanel. Code optimisation Performance improvement

查看:87
本文介绍了C# - 如何在TableLayoutPanel中显示对象数据。代码优化性能改进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们好,



我编写了以下代码来填充tablelayoutpanel控件中对象的数据。它工作:),但当它将数据加载到表上时,它闪烁/跳跃几秒钟,然后2-3秒后,当它完成处理数据时,它填充数据:(。我相信这种行为是因为为基于对象数据动态处理和绘制表中各种控件而编写的代码。



我需要你的帮助来优化代码/提高性能这段代码可以让表格顺利,快速地加载。请帮忙。谢谢。



PS:这段代码是为包含少量数据的表格编写的。转发相同的计划用于填充表格,增加4倍的数据。如果是这种情况,那么性能将非常差,这让我很担心。请提出一些建议。



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.

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);
        }

推荐答案

尝试将生成新控件的代码放在SuspendLayout / ResumeLayout对之间:



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

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.


这篇关于C# - 如何在TableLayoutPanel中显示对象数据。代码优化性能改进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆