QA@IT
«回答へ戻る

回答を投稿

下記で実施たいことの上二つはできませんか?

HTML-------------------------------
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="表示するデータ レコードがありません。" OnPreRender="GridView1_PreRender">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
                <asp:BoundField DataField="num" HeaderText="num" ReadOnly="True" SortExpression="num" />
                <asp:BoundField DataField="item" HeaderText="item" SortExpression="item" />
                <asp:BoundField DataField="qty" HeaderText="qty" SortExpression="qty" />
                <asp:BoundField DataField="unit" HeaderText="unit" SortExpression="unit" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:griddemo_csConnectionString1 %>" ProviderName="<%$ ConnectionStrings:griddemo_csConnectionString1.ProviderName %>" SelectCommand="SELECT [id], [num], [item], [qty], [unit] FROM [invlines]"></asp:SqlDataSource>
    </div>
    </form>
</body>

C#-------------------------------
    protected void GridView1_PreRender(object sender, EventArgs e)
        {
            GridView gv = (GridView)sender;

            //GridViewにヘッダーを1行追加する。
            GridViewRow gvrHeader = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

            TableCell cell1 = new TableCell();
            TableCell cell2 = new TableCell();
            //TableCell cell3 = new TableCell();
            TableCell cell4 = new TableCell();
            TableCell cell5 = new TableCell();

            cell1.Text = "Cell1";
            cell2.Text = "Cell2";
            cell2.ColumnSpan = 2;
            //cell3.Text = "Cell3";   //cell2をColumnSpan = 2 にしたからとりあえずいらない;
            cell4.Text = "Cell4";
            cell5.Text = "Cell5";

            gvrHeader.Cells.Add(cell1);
            gvrHeader.Cells.Add(cell2);
            //gvrHeader.Cells.Add(cell3);   //cell2をColumnSpan = 2 にしたからとりあえずいらない;
            gvrHeader.Cells.Add(cell4);
            gvrHeader.Cells.Add(cell5);

            gv.Controls[0].Controls.AddAt(1, gvrHeader);
        }
下記で実施たいことの上二つはできませんか?

```HTML
HTML-------------------------------
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="表示するデータ レコードがありません。" OnPreRender="GridView1_PreRender">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
                <asp:BoundField DataField="num" HeaderText="num" ReadOnly="True" SortExpression="num" />
                <asp:BoundField DataField="item" HeaderText="item" SortExpression="item" />
                <asp:BoundField DataField="qty" HeaderText="qty" SortExpression="qty" />
                <asp:BoundField DataField="unit" HeaderText="unit" SortExpression="unit" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:griddemo_csConnectionString1 %>" ProviderName="<%$ ConnectionStrings:griddemo_csConnectionString1.ProviderName %>" SelectCommand="SELECT [id], [num], [item], [qty], [unit] FROM [invlines]"></asp:SqlDataSource>
    </div>
    </form>
</body>

C#-------------------------------
    protected void GridView1_PreRender(object sender, EventArgs e)
        {
            GridView gv = (GridView)sender;

            //GridViewにヘッダーを1行追加する。
            GridViewRow gvrHeader = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

            TableCell cell1 = new TableCell();
            TableCell cell2 = new TableCell();
            //TableCell cell3 = new TableCell();
            TableCell cell4 = new TableCell();
            TableCell cell5 = new TableCell();

            cell1.Text = "Cell1";
            cell2.Text = "Cell2";
            cell2.ColumnSpan = 2;
            //cell3.Text = "Cell3";   //cell2をColumnSpan = 2 にしたからとりあえずいらない;
            cell4.Text = "Cell4";
            cell5.Text = "Cell5";

            gvrHeader.Cells.Add(cell1);
            gvrHeader.Cells.Add(cell2);
            //gvrHeader.Cells.Add(cell3);   //cell2をColumnSpan = 2 にしたからとりあえずいらない;
            gvrHeader.Cells.Add(cell4);
            gvrHeader.Cells.Add(cell5);

            gv.Controls[0].Controls.AddAt(1, gvrHeader);
        }