任何优化填充tableLayoutPanel的方法



我有一个tableLayoutPanel,我使用的Windows窗体。该控件由保存sql server数据的datatable填充。我已经确认select声明不是问题。

数据表经常更新,因此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行,我只在Form构造函数中执行下面的代码一次,但我没有看到太多的好处:

public FormDoctorMonitor()
{
    tableLayoutPanel1.RowStyles.Clear();
    tableLayoutPanel1.RowCount = 8; 
    FillTlp();
}

我还可以如何优化tableLayoutPanel的填充?

谢谢。

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

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

After try this:

            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"已经存在,你可以用你的tablelayoutpanel代替"this"

相关内容

最新更新