0

如何在运行时在 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);   

有比这更好的主意的人吗?

4

1 回答 1

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

这个链接实际上帮助我了解了

于 2013-10-30T13:40:56.297 回答