如何在运行时在gridview标题中执行Rowspan和Columnspan?
目前我正在进行一个复杂的过程,首先创建一个表,然后将所有行和列添加到其中。
稍后执行gridview标头的colspan并在行创建的事件中将表添加到其中。
代码段:
Table headerTable = new Table();
headerTable.CssClass = "tbl";
headerTable.CellPadding = 1;
headerTable.CellSpacing = 0;
TableRow headerRow1 = new TableRow();
TableHeaderCell headerTableCell = new TableHeaderCell();
headerTableCell.RowSpan = 2;
headerTableCell.Text = "SL No";
headerTableCell.CssClass = "inputFromTo";
headerRow1.Controls.Add(headerTableCell);
headerTableCell = new TableHeaderCell();
headerTableCell.ColumnSpan = 3;
headerTableCell.Text = "MH1 <br> M. Mark";
headerRow1.Controls.Add(headerTableCell);
for (int i = 1; i <= Convert.ToInt16(txtHHColumn.Text); i++)
{
headerTableCell = new TableHeaderCell();
headerTableCell.ColumnSpan = 3;
headerTableCell.Text = "HH-" + i + "<br> M. Mark";
headerRow1.Controls.Add(headerTableCell);
}
headerTableCell = new TableHeaderCell();
headerTableCell.ColumnSpan = 3;
headerTableCell.Text = "MH2 <br> M. Mark";
headerRow1.Controls.Add(headerTableCell);
TableRow headerRow2 = new TableRow();
TableHeaderCell headerCell1;
TableHeaderCell headerCell2;
TableHeaderCell headerCell3;
for (int i = 1; i < Convert.ToInt16(3 + Convert.ToInt16(txtHHColumn.Text)); i++)
{
headerCell1 = new TableHeaderCell();
headerCell2 = new TableHeaderCell();
headerCell3 = new TableHeaderCell();
headerCell1.Text = "D Entry";
headerCell1.CssClass = "inputFromTo";
headerCell2.Text = "D Exit";
headerCell2.CssClass = "inputFromTo";
headerCell3.Text = "Slack";
headerCell3.CssClass = "inputFromTo";
headerRow2.Controls.Add(headerCell1);
headerRow2.Controls.Add(headerCell2);
headerRow2.Controls.Add(headerCell3);
}
headerTable.Rows.Add(headerRow1);
headerTable.Rows.Add(headerRow2);
GridViewRow HeaderRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell Cell_Header = new TableCell();
Cell_Header.ColumnSpan = 10 + (Convert.ToInt16(txtHHColumn.Text) * 3);
Cell_Header.Controls.Add(headerTable);
HeaderRow.Cells.Add(Cell_Header);
gvOfcManHole.Controls[0].Controls.AddAt(0, HeaderRow);
任何人都有比这更好的想法吗?
答案 0 :(得分:5)
是的,我找到了解决我自己问题的方法
protected void gvOfcManHole_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow headerRow1 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
GridViewRow headerRow2 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableHeaderCell headerTableCell = new TableHeaderCell();
headerTableCell.RowSpan = 2;
headerTableCell.Text = "SL No";
headerRow1.Controls.Add(headerTableCell);
headerTableCell = new TableHeaderCell();
headerTableCell.ColumnSpan = 3;
headerTableCell.Text = "MH1 <br> M. Mark";
headerRow1.Controls.Add(headerTableCell);
//for (int i = 1; i <= Convert.ToInt16(txtHHColumn.Text); i++)
for (int i = 1; i <= 1; i++)
{
headerTableCell = new TableHeaderCell();
headerTableCell.ColumnSpan = 3;
headerTableCell.Text = "HH" + i + "<br> M. Mark";
headerRow1.Controls.Add(headerTableCell);
}
headerTableCell = new TableHeaderCell();
headerTableCell.ColumnSpan = 3;
headerTableCell.Text = "MH2 <br> M. Mark";
headerRow1.Controls.Add(headerTableCell);
TableHeaderCell headerCell1;
TableHeaderCell headerCell2;
TableHeaderCell headerCell3;
//for (int i = 1; i < Convert.ToInt16(3 + Convert.ToInt16(txtHHColumn.Text)); i++)
for (int i = 1; i < Convert.ToInt16(3 + 1); i++)
{
headerCell1 = new TableHeaderCell();
headerCell2 = new TableHeaderCell();
headerCell3 = new TableHeaderCell();
headerCell1.Text = "D Entry";
headerCell2.Text = "D Exit";
headerCell3.Text = "Slack";
headerRow2.Controls.Add(headerCell1);
headerRow2.Controls.Add(headerCell2);
headerRow2.Controls.Add(headerCell3);
}
gvOfcManHole.Controls[0].Controls.AddAt(0, headerRow2);
gvOfcManHole.Controls[0].Controls.AddAt(0, headerRow1);
}
}