Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有办法优化填充tableLayoutPanel?_C#_Winforms_Visual Studio 2010_Tablelayoutpanel_Flowlayoutpanel - Fatal编程技术网

C# 有没有办法优化填充tableLayoutPanel?

C# 有没有办法优化填充tableLayoutPanel?,c#,winforms,visual-studio-2010,tablelayoutpanel,flowlayoutpanel,C#,Winforms,Visual Studio 2010,Tablelayoutpanel,Flowlayoutpanel,我有一个与Windows窗体一起使用的tableLayoutPanel。控件是从保存sql server数据的数据表中填充的。我已经确认select语句不是问题所在 datatable经常更新,因此tableLayoutPanel也经常更新。它基本上工作得很好,但到了一定程度,它会变得稍微慢一点,而且闪烁更加明显 每次我需要刷新控件时,都会执行以下代码: public void FillTlp() { tableLayoutPanel1.Controls.Clear(); tab

我有一个与Windows窗体一起使用的
tableLayoutPanel
。控件是从保存sql server数据的
数据表中填充的。我已经确认
select
语句不是问题所在

datatable经常更新,因此
tableLayoutPanel
也经常更新。它基本上工作得很好,但到了一定程度,它会变得稍微慢一点,而且闪烁更加明显

每次我需要刷新控件时,都会执行以下代码:

public void FillTlp()
{
    tableLayoutPanel1.Controls.Clear();
    tableLayoutPanel1.ColumnStyles.Clear();

    foreach (DataRow r in DT.Rows)
    {
        UcColor button = new UcColor(r);
        tableLayoutPanel1.Controls.Add(button);//, colNumNew, rowNum);
    }
    this.Controls.Add(tableLayoutPanel1);
}     
由于总是有8行,我只在表单构造函数中执行了一次以下代码,但我看不到有什么好处:

public FormDoctorMonitor()
{
    tableLayoutPanel1.RowStyles.Clear();
    tableLayoutPanel1.RowCount = 8; 
    FillTlp();
}
我还可以如何优化填充
表格布局面板


谢谢。

当我有一些显示冻结时,我使用这些扩展控制方法:

public static class ExtensionOfControl
{
    private const int WM_SETREDRAW = 11;
    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParmam);

    public static void SuspendDrawing(this Control parent)
    {
        SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
    }
    public static void ResumeDrawing(this Control parent)
    {
        SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
        parent.Refresh();
    }

    public static void RunWithDrawingSuspended(this Control ctrl, Action code)
    {
        ctrl.SuspendDrawing();
        try
        {
            code();
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            ctrl.ResumeDrawing();
        }
    }
}
尝试以下操作后:

            this.RunWithDrawingSuspended(() =>
        {
            tableLayoutPanel1.Controls.Clear();
            tableLayoutPanel1.ColumnStyles.Clear();

            foreach (DataRow r in DT.Rows)
            {

                UcColor button = new UcColor(r);
                tableLayoutPanel1.Controls.Add(button);//, colNumNew, rowNum);
            }
            this.Controls.Add(tableLayoutPanel1);
        });

如果“this”上已经存在“this”,也许可以用tablelayoutpanel替换“this”

明显的问题:为什么“refresh”实际上是“从头开始构造”?只改变需要改变的。如果没有改变,很难说问题出在哪里。只是一些提示:1-在向面板添加新控件之前,首先
Dispose
您添加的以前的控件。目前您只删除它们。您应该同时执行删除和处置。2-调用
panel.SuspendLayout()在移除控件并调用
面板之前。恢复布局(true)添加新控件后可能会有所帮助。@RezaAghaei,我应该在哪里添加
tableLayoutPanel1.Dispose()?如果我在开头添加它,我将得到运行时错误
无法访问已处置的对象
。我已经用你的建议修改了示例代码;请让我知道他们是否在正确的位置。@rbhatup请不要编辑问题。当前编辑的代码将不起作用。最好是回滚您的编辑。使用TLP实现您自己的网格控制从来都不是一个错误。使用ListView或DataGridView或去购物。如果你要去,你必须给他们信用。对不起,科迪·格雷。。。在我想要的时候,我不想记录找到的代码的引用。但是扩展方法是我的想法;)虽然其他人可能也会想