C# 如何合并表格布局中的两个单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9390641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:10:25  来源:igfitidea点击:

How to merge two cells in Table Layout

c#winformsc#-4.0tablelayoutpanel

提问by Abhijit Shelar

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

我有两行两列。我希望两个单元格的最后一列合并为一列。由于要求,我不使用其他设计选项意味着两个表格布局,其中第一个表格布局有两行。我在 C# 中使用 Winforms。

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

采纳答案by Kamilos

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

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

For example You can set RowSpan poperty in TableLayoutPanel control.

例如您可以在 TableLayoutPanel 控件中设置 RowSpan poperty。

回答by mackjazzy

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.

在将开始合并表格的单元格中设置控件的 RowSpan 属性。即 RowSpan 为 3 将使控件填充其单元格和下面的 2 个单元格。

ColumnSpan to merge to right.

ColumnSpan 合并到右侧。

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

在代码中,调用 SetRowSpan 和/或 SetColumnSpan 方法。

回答by NL3294

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

回答by GregNash

Instead of setting the ColumnSpan/RowSpanproperty, you can add a TableLayoutPanelwithin 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.

您可以在另一个TableLayoutPanel的单元格内添加一个TableLayoutPanel,而不是设置ColumnSpan/ RowSpan属性。您不是合并两个单元格,而是拆分两个单元格。在您在问题中提供的示例中,您会将左列拆分为两行,而不是将右列合并为一行。

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

仅当您计划将CellBorderStyle属性设置为“ None”以外的内容时,此方法才有用。我在这里找到了这个答案,其中CSharpFreak还提出了另一种我没有尝试过的方法。

回答by zurfyx

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

您可以将此类“合并”属性设置为控件:

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

假设 Control 是 aLabel并且您想要合并行,那么您可以按以下方式进行:

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);

回答by Shekhar

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);

回答by Boris Zinchenko

  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
  1. 将任何控件放入表单设计器的单元格中
  2. 选择控件并查看其属性
  3. 在“布局”部分找到“ColumnSpan”属性
  4. 为此值输入所需的列跨度

See picture for illustration:

参见图片说明:

enter image description here

在此处输入图片说明