Abhijit Shelar
Abhijit Shelar

Reputation: 1065

How to merge two cells in Table Layout

I have two rows and two columns. I want last column of both cells merge into one. Due to requirement I do not use other design options means two tablelayouts in which first table layout has two rows.I am use Winforms in C#.

|                       |                    |
|                       |                    |
|                       |                    |
|_______________________|                    |
|                       |                    |
|                       |                    |
|                       |                    |

Upvotes: 35

Views: 65012

Answers (7)

Boris Zinchenko
Boris Zinchenko

Reputation: 2282

  1. Put any control into a cell in form designer
  2. Select the control and view its properties
  3. Find "ColumnSpan" property in "Layout" section
  4. Input desired column span for this value

See picture for illustration:

enter image description here

Upvotes: 56

Shashank Shekhar
Shashank Shekhar

Reputation: 4178

The following code should allow you to span a control across desired number of rows/columns

TableLayoutPanel tableLayoutPanel1 = new TableLayoutPanel(); // not required if you already have the control added else where or in designer. 
TextBox textBox1 = new TextBox(); // not required if you already have the control added else where or in designer. 
tableLayoutPanel1.Controls.Add(textBox1);// not required if you already have the control added else where or in designer. 
tableLayoutPanel1.SetColumnSpan(textBox1, 2);
tableLayoutPanel1.SetRowSpan(textBox1, 2);

Upvotes: 0

zurfyx
zurfyx

Reputation: 32767

You can set such "merging" property to the Control:

Let's say the Control is a Label and you want to merge rows, then you can do it as following:

TableLayoutPanel table = new TableLayoutPanel();

Label lbl = new Label();
lbl.Text = "test";
lbl.Dock = DockStyle.Fill;

table.Controls.Add(lbl, 0, 0); //initial position
table.SetRowSpan(lbl,2);

Upvotes: 0

GregNash
GregNash

Reputation: 1336

Instead of setting the ColumnSpan/RowSpan property, you can add a TableLayoutPanel within the cell of another TableLayoutPanel. Instead of merging two cells, you are then splitting two cells. In the example you provide in your question, you would be splitting the left column into two rows, instead of merging the right column into one row.

This method is only advantageous if you plan to set the CellBorderStyle property to something other than "None". I found this answer here, where CSharpFreak also suggests another method, which I didn't try.

Upvotes: 1

NL3294
NL3294

Reputation: 1514

Here's how to do it in code

//create a label control, add it to the tableLayoutPanel, and merge it into 3 cells.
Label lbl = new Label();
lbl.Location = new Point(0, 0);
lbl.Text = "This is a test label";
MyTableLayoutPanel.Controls.Add(lbl, 0,0);  //start it in cell 0,0
MyTableLayoutPanel.SetColumnSpan(lbl, 3);  //merge 3 columns

Upvotes: 9

mackjazzy
mackjazzy

Reputation: 50

Set the RowSpan property of the control in the cell that will start the merge in the table. i.e. RowSpan of 3 will have the control fill its cell and the 2 cells below.

ColumnSpan to merge to right.

In code, call the SetRowSpan and/or SetColumnSpan method.

Upvotes: 0

Kamilos
Kamilos

Reputation: 805

http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.aspx

For example You can set RowSpan poperty in TableLayoutPanel control.

Upvotes: 6

Related Questions