【问题标题】:How to achieve multiple colspan and multiple header row using ASP.Net gridview?如何使用 ASP.Net gridview 实现多个 colspan 和多个标题行?
【发布时间】:2016-11-03 17:33:49
【问题描述】:

我的情况是需要使用 ASP.Net gridview 实现多个 colspan 和两个标题行。

如下所示

+----------+--------------------------------+--------------------------------+
|Name      |English                         |Math                            |
|          |----------+----------+----------+----------+----------+----------+
|          |1st Term  |2nd Term  |3rd Term  |1st Term  |2nd Term  |3rd Term  |    
+----------+----------+----------+----------+----------+----------+----------+
|Adam      |50        |60        |70        |55        |65        |75        |
+----------+----------+----------+----------+----------+----------+----------+
|Smith     |52        |62        |72        |57        |68        |70        |
+----------+----------+----------+----------+----------+----------+----------+

有可能吗?

编辑:行数和/或列数甚至标题的子标题不固定。

【问题讨论】:

    标签: c# asp.net gridview html-table


    【解决方案1】:

    通过Cells 属性访问网格视图的RowSpanColumnSpan

    访问标题单元格:

    //Replace ColumnSpan with RowSpan if needed (and if you can get multiple header rows)
    Gridview1.HeaderRow.Cells[CELL_INDEX].ColumnSpan = 2; //Need to know the cell index
    

    访问正常行的单元格:

    //Replace ColumnSpan with RowSpan if needed
    Gridview1.Rows[ROW_INDEX].Cells[CELL_INDEX].ColumnSpan = 2; //Need to know the row index and the cell index in that row
    

    多标题行可能有点棘手。我从来没有做过,但这里的人似乎有一个很好的答案:How to add Header and Subheader in Gridview

    【讨论】:

    • 我也看到了答案。但我的问题是我在运行时获取了所有列。因此,即使存在多少列,我也不知道列的名称。
    • @ParvezMRobin 我为动态标题找到了这个。希望这可以帮助你:codeproject.com/Articles/27824/…
    【解决方案2】:

    这是可以做到的,但要得到你想要的设计需要一些试验和错误。使用 GridView OnRowDataBound 事件。在构建 GridView 之后执行此操作会更容易,尤其是对于 RowSpan

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            int spanColumn_1_start = 1;
            int spanColumn_1_length = 3;
    
            //apply colspan
            e.Row.Cells[spanColumn_1_start].ColumnSpan = 3;
    
            //remove the spanned cells
            for (int i = 1; i < spanColumn_1_length; i++)
            {
                e.Row.Cells.RemoveAt(spanColumn_1_start + 1);
            }
    
            //note that the startindex of the 2nd colspan is set after removing cells for 1st colspan
            int spanColumn_2_start = 2;
            int spanColumn_2_length = 3;
    
            //apply colspan
            e.Row.Cells[spanColumn_2_start].ColumnSpan = 3;
    
            //remove the spanned cells
            for (int i = 1; i < spanColumn_2_length; i++)
            {
                e.Row.Cells.RemoveAt(spanColumn_2_start + 1);
            }
        }
        else if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int rowIndex = e.Row.DataItemIndex;
    
            //to make a rowspan you have to work backwards since the next row does not exist yet
            if (rowIndex == 1)
            {
                GridView1.Rows[rowIndex - 1].Cells[0].RowSpan = 2;
                e.Row.Cells.RemoveAt(0);
            }
    
            //span 4 rows in column 3 starting at row 6
            if (rowIndex == 9)
            {
                    int rowSpan = 4;
                    int columnIndex = 3;
    
                    //apply rowspan
                    GridView1.Rows[rowIndex - rowSpan].Cells[columnIndex].RowSpan = rowSpan;
    
                    //remove the spanned rows
                    for (int i = 1; i < rowSpan; i++)
                    {
                        GridView1.Rows[rowIndex - (rowSpan - i)].Cells.RemoveAt(columnIndex);
                    }
            }
        }
    }
    

    上面的sn-p会给出如下结果。

    更新

    要添加额外的标题行,您需要使用 GridView 的 OnRowCreated 事件。

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //cast the sender back to a gridview
        GridView gv = sender as GridView;
    
        //check if the row is the header row
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //create a new row
            GridViewRow extraHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            extraHeader.BackColor = Color.Green;
    
            //loop all the columns and create a new cell for each
            for (int i = 0; i < gv.Columns.Count; i++)
            {
                TableCell cell = new TableCell();
                cell.Text = "ExtraHeader " + i;
    
                //add the cell to the new header row
                extraHeader.Cells.Add(cell);
            }
    
            //add the new row to the gridview
            gv.Controls[0].Controls.AddAt(0, extraHeader);
        }
    }
    

    【讨论】:

    • 其实这不是我问的。我想实现多个标题行。但是您所做的是创建了一个带有多个 colspan 的单个标题行。是的,我需要这个。但另外我需要一个标题行,其中包含每一列的标题。
    • 你现在知道如何使用我的 sn-p 做到这一点,只需稍微调整一下。
    • @VDWWD 从你的 sn-p 看如何添加额外的标题行完全不明显
    • 添加了 sn-p 以创建额外的标题行。
    猜你喜欢
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多