none
GridViewのHeaderRowについて RRS feed

  • 質問

  • はじめまして。

    VisualStudio2005のVB.NETでASP.NETアプリケーションを開発しています。

    GridViewのHeaderRowで、以下のHTMLのようなタイトル行は実現可能でしょうか?

    ご存知の方がいらっしゃったら、ご教示お願い致します。

    (行、列の連結はGridView.HeaderRow.Cells(0).ColumnSpanやRowSpanで可能でしたが、行数を増やす方法がわかりませんでした)

     

    サンプルHTML

    <table border=1>
     <tr>
      <th rowspan=2>1</th><th colspan=2>2グループ</th>
     </tr>
     <tr>
      <th>2-1</th><th>2-2</th>
     </tr>
    </table>

    2006年3月23日 11:28

回答

  • 実際に試してませんが、以下のような感じでいけるんじゃないかと思います。



    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer || e.Row.RowType == DataControlRowType.EmptyDataRow)
        {
            //ヘッダ 2行目
            TableCell cell21 = new TableCell();
            cell21.Font.Size = FontUnit.Parse("12pt"); ;
            cell21.Text = "2-1";
     
             TableCell cell22 = new TableCell();
            cell22.Font.Size = FontUnit.Parse("12pt"); ;
            cell22.Text = "2-2";
     
             GridViewRow row2 = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
            row2.Cells.Add(cell21);
            row2.Cells.Add(cell22);
     
            GridView1.Controls[0].Controls.AddAt(0, row2);
     
             //ヘッダ 1行目
            TableCell cell11 = new TableCell();
            cell11.Font.Size = FontUnit.Parse("12pt");
            cell11.RowSpan = 2;
            cell11.Text = "1";
     
             TableCell cell12 = new TableCell();
            cell12.Font.Size = FontUnit.Parse("12pt"); ;
            cell12.ColumnSpan = 2;
            cell12.Text = "2グループ";
     
             GridViewRow row1 = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
            row1.Cells.Add(cell11);
            row1.Cells.Add(cell12);
     
            GridView1.Controls[0].Controls.AddAt(0, row1);
        }
    }


     

    2006年3月23日 11:57
    モデレータ

すべての返信

  • 実際に試してませんが、以下のような感じでいけるんじゃないかと思います。



    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer || e.Row.RowType == DataControlRowType.EmptyDataRow)
        {
            //ヘッダ 2行目
            TableCell cell21 = new TableCell();
            cell21.Font.Size = FontUnit.Parse("12pt"); ;
            cell21.Text = "2-1";
     
             TableCell cell22 = new TableCell();
            cell22.Font.Size = FontUnit.Parse("12pt"); ;
            cell22.Text = "2-2";
     
             GridViewRow row2 = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
            row2.Cells.Add(cell21);
            row2.Cells.Add(cell22);
     
            GridView1.Controls[0].Controls.AddAt(0, row2);
     
             //ヘッダ 1行目
            TableCell cell11 = new TableCell();
            cell11.Font.Size = FontUnit.Parse("12pt");
            cell11.RowSpan = 2;
            cell11.Text = "1";
     
             TableCell cell12 = new TableCell();
            cell12.Font.Size = FontUnit.Parse("12pt"); ;
            cell12.ColumnSpan = 2;
            cell12.Text = "2グループ";
     
             GridViewRow row1 = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
            row1.Cells.Add(cell11);
            row1.Cells.Add(cell12);
     
            GridView1.Controls[0].Controls.AddAt(0, row1);
        }
    }


     

    2006年3月23日 11:57
    モデレータ
  • trapemiya様

    ご回答有難う御座います。

    サンプルとして書き込んで頂いたコードを参考に、VB.NETの文法に修正して、思ったとおりの表示になりました。有難う御座いました。

    一応、コードも以下に書き込んでおきます。
    (ヘッダの1行目と2行目のコード記述の順序も変えてみました)


    If (e.Row.RowType = DataControlRowType.Footer Or e.Row.RowType = DataControlRowType.EmptyDataRow) Then
        ' ヘッダ1行目
        Dim cell11 As New TableCell
        cell11.RowSpan = 2
        cell11.Text = "1"

        Dim cell12 As New TableCell
        cell12.ColumnSpan = 2
        cell12.Text = "2グループ"

        Dim row1 As New GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)
        row1.Cells.Add(cell11)
        row1.Cells.Add(cell12)

        GridView1.Controls(0).Controls.AddAt(0, row1)

        ' ヘッダ2行目
        Dim cell21 As New TableCell
        cell21.Text = "2-1"

        Dim cell22 As New TableCell
        cell22.Text = "2-2"

        Dim row2 As New GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)
        row2.Cells.Add(cell21)
        row2.Cells.Add(cell22)

        GridView1.Controls(0).Controls.AddAt(1, row2)
    End If

    2006年3月24日 1:08