-2

I have a tableLayoutPanel that I use with a Windows Form. The control is populated from a datatable which holds sql server data. I've already confirmed that the select statement is not the issue.

The datatable is frequently updated, so the tableLayoutPanel is also frequently updated. It's essentially working well, but it gets to a point that it becomes a little slower and that flickering is more noticeable.

Everytime I need to refresh the control, this code is executed:

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

Since the will always have 8 rows, I execute the following code in the Form constructor only once, but I don't see much benefit:

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

How else can I optimize populating the tableLayoutPanel?

Thanks.

fdkgfosfskjdlsjdlkfsf
  • 3,165
  • 2
  • 43
  • 110
  • Obvious question: why is "refresh" actually "construct from scratch"? Change only what needs changed. – DonBoitnott Jun 30 '16 at 13:22
  • Without having [MCVE](http://stackoverflow.com/help/mcve) it's hard to say what the problem is. Just some tips: **1-** Before adding new controls to the panel, first `Dispose` previous controls which you added. Currently you only remove them. You should perform both removing and disposing. **2-** Calling `panel.SuspendLayout();` before removing controls and calling `panel.ResumeLayout(true);` after adding new controls may help. – Reza Aghaei Jun 30 '16 at 13:27
  • @RezaAghaei, where should I add `tableLayoutPanel1.Dispose();`? If I add it at the beginning, I will get runtime error `Cannot access disposed object`. I've modified the sample code with your suggestions; please let me know if they're in the right place. – fdkgfosfskjdlsjdlkfsf Jun 30 '16 at 14:30
  • @rbhatup please don't edit the question. Currently the edited code will not work. It's better to rollback your edit. – Reza Aghaei Jun 30 '16 at 14:34
  • @DonBoitnott, what do you mean by _Change only what needs changed._? – fdkgfosfskjdlsjdlkfsf Jun 30 '16 at 14:34
  • Surely you should not dispose the panel, But dispose its child controls. `foreach (Control item in panel.Controls) { panel.Controls.Remove(item); item.Dispose(); }` – Reza Aghaei Jun 30 '16 at 14:35
  • 2
    Implementing your own grid control with TLP is never not a mistake. Use ListView or DataGridView or go shopping. – Hans Passant Jun 30 '16 at 14:39
  • @HansPassant, are you saying that [something like this, with a label in each cell](http://i.stack.imgur.com/mfv0R.png), is easier with a DataGridView ? One of the reasons for using the panel is because this couldn't easily be done with a `DataGridView`. – fdkgfosfskjdlsjdlkfsf Jun 30 '16 at 14:47
  • It could be easily done using datagridview. Why couldn't be done? – Reza Aghaei Jun 30 '16 at 14:50
  • I posted something similar [here](http://stackoverflow.com/questions/36991351/accomplish-gallery-type-display-with-a-windows-forms-data-control-and-datatable), and I was told by SO users that it would not be something easy to achieve. The only difference between this and the previous question was that the other one was clickable. – fdkgfosfskjdlsjdlkfsf Jun 30 '16 at 14:54
  • It depends on rows and columns count.. Also it depends to the complexity of what you want to show in cell. Your linked image can be easily done. Also the linked post is OK. – Reza Aghaei Jun 30 '16 at 14:56
  • @RezaAghaei, you also posted an answer from a user with a [similar requirement](http://stackoverflow.com/questions/33968993/how-to-create-a-magic-square-using-windows-forms), and you suggested the panel. – fdkgfosfskjdlsjdlkfsf Jun 30 '16 at 14:59
  • There's nothing I'd like more than to use a DataGridView, but I haven't found a single link showing me how to achieve something like that with this control. – fdkgfosfskjdlsjdlkfsf Jun 30 '16 at 15:01
  • @rbhatup As I said in previous comment, It depends on rows and columns count. a Magic square is a good example of `TableLayoutPanel`. But if you want to use a `DataTable` with lots of rows will you use TableLayoutPanel? – Reza Aghaei Jun 30 '16 at 15:01
  • Surely the problem of showing some colored label, can be easily solved using both `TableLayoutPanel` and `DataGridView`. – Reza Aghaei Jun 30 '16 at 15:08

1 Answers1

-1

When i have some display freezing, i use those extension methode of control :

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

Maybe you can replace "this" by your tablelayoutpanel if already exist on "this"

lolo
  • 101
  • 1
  • 6
  • If you are going to [take code from somebody else](http://stackoverflow.com/questions/487661/how-do-i-suspend-painting-for-a-control-and-its-children), you are required to give them credit for it. – Cody Gray - on strike Jun 30 '16 at 14:54
  • sorry cody gray... at the time I wanted, I did not think to note the references of the found code. but the extension method is my idea ;) although other there will probably also thought – lolo Jul 22 '16 at 13:42