3 Replies - 1346 Views - Last Post: 05 March 2013 - 07:32 AM Rate Topic: -----

#1 scienalc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 22-July 11

how to dispose controls from a tablelayoutpanel and redraw them

Posted 04 March 2013 - 04:51 PM

hello, everyone

the title is pretty self explanatory, namely, I have database entries (which are held in a DataTable object) and I dynamically generate controls (lavels, textboxes, comboboxes, ...) in a tablelayoutpanel to display them.

However, I have encountered problems with greater amounts of data, i.e. windows handles run out and the general overview suffers, so I intend to create a page concept. For example, page 1 will show the first 10 rows from the DataTable, page 2 will show the second 10 rows from the DataTable, and so on. In order to make this work, I have to dispose the controls from one page and repopulate the tablelayoutpanel with desired data. I tried something like:

foreach (Control ctrl in tablelayoutpanel.controls)
{
    ctrl.dispose();
}



It does remove the controls, but when I try to redraw, weird stuff happens, e.g. I get a chess board like structure, where in the the odd rows only controls on odd indexes are displayed and in even rows only on even indexes.

More confusing is that the exactly same draw method is used when initializing the layoutpanel and redrawing it, so I don't see why it would create different results after the present controls are removed.

So basically, one could say, that I'm looking for a way to return my layoutpanel to a 'tabula rasa' state, i.e. to remove all controls from it.

Can anybody help with that?



PS: If I would simply dispose of the tablelayoutpanel itself, would that also release the resources of its controls?

Is This A Good Question/Topic? 0
  • +

Replies To: how to dispose controls from a tablelayoutpanel and redraw them

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: how to dispose controls from a tablelayoutpanel and redraw them

Posted 04 March 2013 - 09:34 PM

If you read the documentation for the TableLayoutPanel, they do not recommend putting that many controls in it in the first place. I forget now what other bit of documentation had it, but if you had a large amount of tabular data, the DataGridView is usually a better fit because it paints the cells of the grid instead of having a control per cell. It's only when a row is activated that the painted cells are replaced with live controls.

Let's say you do ignore that advice about the table layout panel, the correct thing to do is first remove the control from the controls collection, and then dispose them. And yes, the alternative is to remove and dispose the TableLayoutPanel.

But any which way, it's a hack. You should consider a redesign and opting to go with the DataGridView.

An even better plan would be to drop your current WinForms code and go with WPF instead so that you don't have to worry about running out window handles.
Was This Post Helpful? 0
  • +
  • -

#3 scienalc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 22-July 11

Re: how to dispose controls from a tablelayoutpanel and redraw them

Posted 05 March 2013 - 02:46 AM

Thanks for the reply and your time. I found that disposing the tablelayoutpanel does the job. It remains to test it with a larger amount of data, just to be sure.

You are generally right about your suggestions. Even if had the time to do such a major redesign, I don't think it would be wise to show the user 30.000 rows in one view (which is a valid scenario, if one chooses to not specify any filters) and the loading time would be unacceptable. So, I would definitely keep the pages solution.

If I had known to what proportions the initial requirements would grow during development, I would have most certainly chosen WPF. However, I'm stuck with what I've got, so I improvise.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: how to dispose controls from a tablelayoutpanel and redraw them

Posted 05 March 2013 - 07:32 AM

If you had structured your program to have clear delineation between data, view, and logic, replacing the current WinForms code with WPF should not be painful.

Anyway, the DataGridView is generally smart about getting and displaying data. It will only render enough that will fit on the screen, albeit it sucking in all the data from the backend. If you feel that the DataGridView is not smart enough, you can switch it to virtual mode and you'll have fine grained control about how much data is fed to the DataGridView. You get the paging effect with regards to your data access patterns, but the user gets the familiar large spreadsheet feel.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1