35

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#.

|                       |                    |
|                       |                    |
|                       |                    |
|_______________________|                    |
|                       |                    |
|                       |                    |
|                       |                    |
wonea
  • 4,783
  • 17
  • 86
  • 139
Abhijit Shelar
  • 1,055
  • 7
  • 21
  • 41

7 Answers7

55
  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

Boris Zinchenko
  • 2,142
  • 1
  • 24
  • 34
  • 1
    For this question should change RowSpan property of control. – Shaho Sep 17 '18 at 13:35
  • @Shaho, thank you. Correct. I captured screen for merging cells in horizontal direction, which was my case when i was searching for a hint and came to this question. I hope it is helpful illustration for either span direction. – Boris Zinchenko Sep 18 '18 at 14:12
9

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
NL3294
  • 984
  • 1
  • 10
  • 27
6

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

For example You can set RowSpan poperty in TableLayoutPanel control.

Kamilos
  • 795
  • 2
  • 10
  • 25
1

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.

GregNash
  • 1,316
  • 1
  • 16
  • 25
0

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.

mackjazzy
  • 48
  • 1
  • 8
0

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);
zurfyx
  • 31,043
  • 20
  • 111
  • 145
0

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);
Shashank Shekhar
  • 3,958
  • 2
  • 40
  • 52