Inserting rows in TableLayoutPanel

Unimatrix_001
Unimatrix_001 used Ask the Experts™
on
Hello,

I'm trying to programatically insert rows in a TableLayoutPanel, which I can do by incrementing the row count and inserting a row style; thing is when I do this it doesn't seem that all the controls below the row I'm inserting don't move down but stay at the same row. Am I doing something wrong, I'm struggling to believe that for as long as WinForms has been about that something like this hasn't cropped up sometime.

Thanks,
Uni
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008

Commented:
If you increment the TableLayoutPanel.RowCount, and add a RowStyle, then you are adding the row to the bottom, so controls won't move down.

Author

Commented:
Well I'm inserting a row style and incrementing the row count - I can't see how else I can do this?
Most Valuable Expert 2012
Top Expert 2008

Commented:
How are your RowStyles configured?  Are you using AutoSize, Absolute, or Percentage?
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Author

Commented:
All RowStyles are autosized.
Most Valuable Expert 2012
Top Expert 2008

Commented:
Can you give me an example of what you are doing?

Author

Commented:
Yep, there's a function which adds a button in the third last row.
private void AddFilterRow(){

	//Change the text on the button on the third last row to Remove filter.
	((Button)TablePanel_Main.GetControlFromPosition(0, TablePanel_Main.RowCount-3)).Text="Remove Filter";

	//Create the new button.
	Button btnNewAddButton=new Button();
	btnNewAddButton.Text="Add Filter";
	btnNewAddButton.AutoSize=true;
	btnNewAddButton.AutoSizeMode=AutoSizeMode.GrowAndShrink;
	btnNewAddButton.Dock=DockStyle.Fill;

	//Create a new row just above the third last one and add this button.
	TablePanel_Main.RowCount++;
	TablePanel_Main.RowStyles.Insert(TablePanel_Main.RowCount-3, new RowStyle(SizeType.AutoSize));
	TablePanel_Main.Controls.Add(btnNewAddButton, 0, TablePanel_Main.RowCount-3);
}

Open in new window

Most Valuable Expert 2012
Top Expert 2008

Commented:
Are you adding controls to the TableLayoutPanel, and then adding an additional row for the Remove Filter option?  Can you show me what that looks like?
Most Valuable Expert 2012
Top Expert 2008

Commented:
Here is the result of my test:


this.tableLayoutPanel1.AutoSize = true;
            this.tableLayoutPanel1.RowCount = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            for (int i = 0; i < 4; i++)
            {
                this.tableLayoutPanel1.RowCount++;
                this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 25f));

                Label label = new Label();
                label.Anchor = AnchorStyles.Left| AnchorStyles.Right;
                label.Text = "Label " + i;

                this.tableLayoutPanel1.Controls.Add(label, 0, i);
            }

            Label remove = new Label();
            remove.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            remove.Text = "Remove";

            this.tableLayoutPanel1.RowStyles.Insert(2, new RowStyle(SizeType.AutoSize));
            this.tableLayoutPanel1.Controls.Add(remove, 0, 2);

Open in new window

Screenshot.png

Author

Commented:
The TableLayoutPanel starts off with 4 columns - which never changes and 3 rows (of which there will never be less).

The last row has a data control spanning the 4 columns.
The middle row has a button in the first column, which should never change. Call this button X
The first row has a button in the first column also, which does change. Call this button A.

The idea being when the button A is pushed a new row is inserted underneath where button A is located and a new instance of button A is added to this row and the original Button A that was pushed has the text changed to "Remove Filter".

I've attached two screen shots showing what happens before the button is pushed and after.


Before.png
After.png

Author

Commented:
Hrm, I notice that you don't ever specifically change the RowCount property - yet I do... If I remove that line then the DataGridLayout the darker blue thing, doesn't ruin in size, but I still can't insert the control without the same thing appearing as in After.png where it places the new control on the same line as Run Filter.

Author

Commented:
Ok - done a little more testing and have the following code, where the last messagebox will actually crash as there is no control there. What I'm not understanding though is why? It should be crashing on the first messagebox as inserting a new row style should push all the components down, meaning row 0 would be empty?

I'll see if I can create a sample project so you can see what I mean.

Thanks,
Uni
TablePanel_Main.RowStyles.Insert(0, new RowStyle(SizeType.AutoSize));

			MessageBox.Show(TablePanel_Main.GetControlFromPosition(0, 0).Name);
			MessageBox.Show(TablePanel_Main.GetControlFromPosition(0, 1).Name);
			MessageBox.Show(TablePanel_Main.GetControlFromPosition(0, 2).Name);
			MessageBox.Show(TablePanel_Main.GetControlFromPosition(0, 3).Name);

			TablePanel_Main.Controls.Add(btnNewAddButton, 0, 3);

Open in new window

Author

Commented:
Here we go...

Thanks,
Uni
SampleProject.zip

Author

Commented:
Done some more digging - it works fine if there is only one column, but as soon as the column count is greater than 1, then components are not pushed down correctly.... :S
Most Valuable Expert 2012
Top Expert 2008
Commented:
1) I have never had to insert a row in a TableLayoutPanel.

2) I did see where it doesn't work when you have multiple columns.

3) I believe that the problem is the way that controls are handled when there are 2 controls in the same cell (not allowed).  When you add a control to the TableLayoutPanel cell, the existing control is moved to the next cell, which means to the next column, not the next row.

4) Is there any way that you could refactor the code to add the "Remove Filter", instead of inserting?

Author

Commented:
Ok.

Thank you. :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial