簡體   English   中英

如何創建動態GridView?

[英]How to create a dynamic gridview?

在此處輸入圖片說明

我希望我的網格視圖看起來像這樣。

這是我的議程表: 在此處輸入圖片說明

如何創建可以如上圖所示的表格視圖/表格。 過去我曾使用簡單的網格視圖,將它們綁定到數據源,但不僅限於此。

您可以通過編程在GridView中使用RowSpan和ColSpan。 您將必須使用GridView的OnRowDataBound事件。

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //chech if the row is the header
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //span 3 columns, starting with the first one (0)
            e.Row.Cells[0].ColumnSpan = 3;

            //remove the other 2 column cells
            e.Row.Cells.RemoveAt(2);
            e.Row.Cells.RemoveAt(1);
        }
        //check if the row is a datarow
        else if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //the last rownumber of the rows to be spanned, can only count backwards because next row does not exist yet.
            if (e.Row.RowIndex == 8)
            {
                //amount of rows to be spanned
                int rowSpanCount = 4;

                //find the first cell counting backwards (8 - rowSpanCount)
                GridViewRow firstRow = GridView1.Rows[e.Row.RowIndex - rowSpanCount];
                firstRow.Cells[1].RowSpan = rowSpanCount;

                //hide the other cells that are part of the rowspan
                for (int i = 1; i < rowSpanCount; i++)
                {
                    GridViewRow nextRow = GridView1.Rows[e.Row.RowIndex - i];
                    nextRow.Cells[1].Visible = false;
                }
            }
        }
    }

在我的代碼段中,HeaderRow將跨越所有3列,而單元格4則將第1列跨越4行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM