有没有什么方法可以优化填充tableLayoutPanel?

时间:2016-06-30 13:09:10

标签: c# winforms visual-studio-2010 tablelayoutpanel flowlayoutpanel

我有一个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

感谢。

1 个答案:

答案 0 :(得分:-1)

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

function addWorkingDates(startDate, days) {
    current_day = startDate.getDay() - 1; // Week day, starting on Monday
    weekend_days = 2*parseInt((current_day + days)/5);
    startDate.setDate(changed_to.getDate() + days + weekend_days);
}

尝试之后:

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

也许你可以替换"这个"如果你的tablelayoutpanel已经存在于"这个"