2016-11-10 6 views
0

現在、このような外観のGridviewを生成しようとしています。特定のテンプレートを使用してプログラムでグリッドビューを生成する

Example

最初の2列は、私は、データベース内の指定されたデータに基づいてつかむだろうBoundfield、で行われます。

しかし、私はAM & PMの行を持っていなければなりません。

だけAM & PMの行を日付のヘッダのためのcolspanを行うと、作成する方法はありますか? AM & PMは列としてカウントされないため、10/11/2016は列です。

ASPX

<asp:GridView ID="gvAttendance" runat="server"> 
        <Columns> 
         <asp:BoundField DataField="traineeID" HeaderText="Trainee ID" /> 
         <asp:BoundField DataField="idNum" HeaderText="ID" /> 
        </Columns> 
       </asp:GridView> 

答えて

0

それは私のスニペットを見て、行って、あなたのニーズに合わせて調整することができます。スニペットは、デフォルトヘッダーにColSpanを適用し、GridViewに追加のヘッダー行を追加します。私は3つの日付の別の追加を持っているので、もし

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.Header) 
    { 
     int spanColumn_1_start = 2; 
     int spanColumn_1_length = 2; 

     //apply colspan 
     e.Row.Cells[spanColumn_1_start].ColumnSpan = spanColumn_1_length; 

     //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 = 3; 
     int spanColumn_2_length = 2; 

     //apply colspan 
     e.Row.Cells[spanColumn_2_start].ColumnSpan = spanColumn_2_length; 

     //remove the spanned cells 
     for (int i = 1; i < spanColumn_2_length; i++) 
     { 
      e.Row.Cells.RemoveAt(spanColumn_2_start + 1); 
     } 
    } 
} 

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    //to add a row above the normal gridview header, use: if (e.Row.RowType == DataControlRowType.Header) 

    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItemIndex == 0) 
    { 
     //cast the sender as gridview 
     GridView gridView = sender as GridView; 

     //create a new gridviewrow 
     GridViewRow gridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert); 

     //add some cells 
     TableCell tableCell = new TableCell(); 
     tableCell.Text = ""; 
     tableCell.ColumnSpan = 2; 
     gridViewRow.Cells.Add(tableCell); 

     tableCell = new TableCell(); 
     tableCell.Text = "AM"; 
     gridViewRow.Cells.Add(tableCell); 

     tableCell = new TableCell(); 
     tableCell.Text = "PM"; 
     gridViewRow.Cells.Add(tableCell); 

     tableCell = new TableCell(); 
     tableCell.Text = "AM"; 
     gridViewRow.Cells.Add(tableCell); 

     tableCell = new TableCell(); 
     tableCell.Text = "PM"; 
     gridViewRow.Cells.Add(tableCell); 

     //add the new row to the gridview 
     gridView.Controls[0].Controls.AddAt(1, gridViewRow); 
    } 
} 

結果は、 `RowCreated`については、この

enter image description here

+0

のように見えるのだろうか?それで、 'AM'' PM'を動的に追加することは可能ですか? – Alvin

+0

とにかく、努力してくれてありがとうと言いたいのですが、私は現在コードを試しています。乾杯。 (: – Alvin

+0

はい、行に6つの 'TableCell'コントロールを追加するだけです。 – VDWWD

関連する問題