views:

27

answers:

1

C#, ASP.NET, VS08, using HTML table.

I want to merge cells - rowspan and colspan spans it but does not merge it.

For a clear picture,

  • drag a HTML table control on the design view
  • select the cells of a column
  • right click, modify, merge

I would like to do this programatically in C# coding on a button click.

+1  A: 

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. This will give you access to the control's server-side 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).

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

It is similar for 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!

kbrimington