简体   繁体   English

如何使用代码合并列中的单元格?

[英]How to merge cells in a column using code behind?

C#, ASP.NET, VS08, using HTML table. C#,ASP.NET,VS08,使用HTML表。

I want to merge cells - rowspan and colspan spans it but does not merge it. 我想合并单元格 - rowspan和colspan跨越它但不合并它。

For a clear picture, 为了清晰的画面,

  • drag a HTML table control on the design view 在设计视图上拖动HTML表格控件
  • select the cells of a column 选择列的单元格
  • right click, modify, merge 右键单击,修改,合并

I would like to do this programatically in C# coding on a button click. 我想在按钮点击的C#编码中以编程方式执行此操作。

It's just a little tedious to do this from code-behind, but the process is simple. 从代码隐藏中做这件事只是有点乏味,但过程很简单。

First off, I'm assuming your table and its elements are marked with the runat="server" attribute. 首先,我假设您的表及其元素标有runat="server"属性。 This will give you access to the control's server-side API. 这将使您可以访问控件的服务器端API。

Say you want to merge two cells in the first row. 假设您要合并第一行中的两个单元格。 The process involves setting the colspan of one cell, then removing the other(s). 该过程涉及设置一个单元的colspan,然后移除其他单元。

myTable.Rows[0].Cells[i].ColSpan = 2;
myTable.Rows[0].Cells.RemoveAt(i + 1)

It is similar for rowspan. 对于rowspan来说类似。

myTable.Rows[0].Cells[i].RowSpan = 2;
myTable.Rows[1].Cells.RemoveAt(i)

Here is an example of a larger merge involving both rowspan and colspan: 下面是包括两个行跨度和列跨度较大的合并的例子:

myTable.Rows[0].Cells[i].ColSpan = 2;
myTable.Rows[0].Cells[i].RowSpan = 2;
myTable.Rows[0].Cells.RemoveAt(i + 1)
myTable.Rows[1].Cells.RemoveAt(i)
myTable.Rows[1].Cells.RemoveAt(i + 1)

Note that if your table already has row spans and cell spans that you will have more footwork to do to calculate which cells need removing. 请注意,如果您的表已经有行跨度和单元格跨度,那么您将需要更多的步法来计算需要删除的单元格。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM