I've been having problems when removing rows from a tableLayoutPanel. Let's say I have a table with 5 rows, then I remove the last row. The result is 4 rows, but it's like the last row's height got increased with whatever the 5th row's value was. I can't seems to decrease it no matter what. Changing the last item's value from RowStyle doesn't take effect. Any ideas?
If you are using the forms designer, then you can right click on the table panel and choose "Edit Rows and Columns". From this dialog you can switch between showing columns or rows using the combo box at the top. Once you are viewing the rows you can set the exact size or proportions of the row. I find this dialog to be particularly useful when working with table layout panels.
I hope this helps and let me know if I have misunderstood.
Actually, I'm not using the designer, I'm coding the table. What I'm trying to do is create a user control of a thumbnail viewer. I exposed a method to remove an item from a given index, and have the items shift to a new cell. The problem is once that method is called enough so that the last row has no items, I remove it, but the height is all wrong. I included some screen grabs to illustrate my point.
The 1st image is before. The 2nd image is after removing two items from the control. I'd expect the scroll bar to snap up where Zapp and Zoidberg are, but it just stays where it was originally. I can't seem to decrease the height of that last row. I even tried setting it to 0 with no luck.
It seems that you have to decrement the RowCount property as well as removing the RowStyle. I got the following code to work:
tableLayoutPanel1.RowCount--;
tableLayoutPanel1.RowStyles.RemoveAt(tableLayoutPanel1.RowCount);
But this doesn't reduce the height of the table as whole. You would also need to recalculate its Height property to fix your scrolling issue.
Also, you must remove any controls from the row before you remove it.
I think I missed recalculating the table's height and just assumed it would do it itself once the row was removed. I'll give it a try when I get home tonight.
So I made a new table to test this out, and it looks like AutoSize needs to be set to true and AutoSizeMode to GrowAndShrink, otherwise it never shrinks.
Hope this helps anyone having the same problem!
Last edited by jasonlabbe; May 3rd, 2010 at 10:32 PM.
It turned out that didn't work at all. It looks like it works when a row gets deleted, but instead of getting a scroll bar, the size of the control just increases. There's a MaximumSize property to cap the table's size, but then removing a row has the same problem!
I tested on a table that had 2 columns and 3 rows, each cell has a button in them. The height of each row is an absolute value of 100, and the table's height is 250. Here's some code I tried, but I just get the result from the 2nd image.
The OP does a better job than me at explaining the issue. Just to sum it up, all that needs to be done is to parent the table layout panel to a panel. Then set the following properties for the table: AutoSize = true, AutoSizeMode = GrowAndShrink, AutoScroll = FALSE. Set AutoScroll to true on the panel.
The result is a table where you can freely add, and remove rows without having to worry about awkward empty space or control resizing. Voilà!
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.