Delete Rows from a TableLayoutPanel

A

avanti

Hi,

I am trying to delete rows from a TableLayoutPanel at Runtime. I tried
editing the RowStyles collection but doesn't do what I want it to. I
have a table with 10 rows. I want to delete rows 2 to 10 at runtime,
keeping the first row unchanged.

Can this be done?

Thanks,
Avanti Ketkar
 
G

Guest

Hi,

I was struggling with the same problem today.
In the first column of my TableLayoutPanel, I'm showing a checkbox.
When the user is checking it, a new row needs to be created.
When the user is unchecking it, the row containing the checkbox should be
removed.

Logically to remove a row in the TableLayoutPanel, I would think to do
something like this:
tableLayoutPanel.RowStyles.RemoveAt(index);

But I found out this is not enough.
You need to clear the controls for that given row as well!
tableLayoutPanel.Controls.RemoveAt(...);
tableLayoutPanel.Controls.RemoveAt(...);

Once the controls in the Controls-collection are gone, the Event that
redraws your tableLayoutPanel doesn't draw them again.

Hopes this is working for you as well...
 
G

Guest

Ok the problem seems to be a little bit more complicated than I thought at
first sight.

Solution, what to do when removing a row:
- decrement tableLayoutPanel.RowCount
- remove rowstyle: tableLayoutPanel.RowStyles.RemoveAt(...)
- remove corresponding controls of removed row in tabelLayoutPanel.Controls
- change RowCounter of controls that come after the removed row

Explanation of last action:
tableLayout.GetRow('control'), where 'control' is a control belonging to a
row that comes after the remove row, will give you his old rowIndex!
For that you need to set the new rowIndex for all controls belonging to a
row that's coming after a removed row.

My code:
for (int counter = controlsCounter; counter <
ux_tlpCostCenters.Controls.Count; counter++)
{
ux_tlpCostCenters.SetRow(ux_tlpCostCenters.Controls[counter],
ux_tlpCostCenters.GetRow(ux_tlpCostCenters.Controls[counter]) - 1);
}

Over here the integer 'controlsCounter' is the beginindex of the first
control in my tableLayoutPanel.Controls that needs a new RowIndex.

This should really help now ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top