2016-10-01 117 views
0

enter image description here如何创建动态gridview?

我希望我的网格视图看起来像这样。

这是我的日程表: enter image description here

如何可以创建可以被看作是在上面的PIC的网格视图/表。 我曾经使用简单的网格视图,将它们绑定到数据源,但这不仅仅是这些。

回答

0

您可以编程方式在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行。