行跨度和列跨度为ASP

行跨度和列跨度为ASP

本文介绍了行跨度和列跨度为ASP .NET的GridView在运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时做ROWSPAN和Columnspan在GridView的头?

How to do Rowspan and Columnspan in gridview header at run time?

目前我正与先创建一个表,然后将所有的行和列到它的复杂过程做。

currently I am doing it with a complex process of creating a table first and then adding all rows and columns into it.

后来做的GridView头的合并单元格并添加表到它的行创建的事件。

Later on doing colspan of gridview header and adding table into it on row created event.

code片断:

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

任何有比这更好的主意吗?

Anyone with better idea than this?

推荐答案

是的,我找到了解决我自己的问题。

Yes i found the solution to my own problem

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

这篇关于行跨度和列跨度为ASP .NET的GridView在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:27