简体   繁体   中英

Add new row into Header of GridView

I have a problem with adding a new row with several controls like textBoxes into Header of GridView. When I add them into Header in GridView_RowCreated method with ...

if (e.Row.RowType == DataControlRowType.Header)
        {
            GridViewRow r = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
            TableCell tc = new TableCell();

.. they always are put into first row, not second. How can I change it? I want my created row put in second Header Row.
I tried to do it by myself, Firstly, I modified ShowHeader into false and add both first and second rows programmatically, but it is wrong way, aspecially when grid has no data to show but it is necessary to show header. I found this discussion similar discussion , but intellisence doesn't know about override PrepareControlHierarchy. I tried to search it with Object browser, and found that this is method of GridView, but I couldn't plug it and test. Maybe somebody knows easier solution for this prob. I need some assist.
UPDATED!!! 在此输入图像描述


 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" DataKeyNames="username" DataSourceID="SqlDataSource1" 
        ForeColor="#333333" GridLines="None" onrowcreated="GridView1_RowCreated">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:TemplateField HeaderText="username" SortExpression="username">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name" SortExpression="Name">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Surname" SortExpression="Surname">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Surname") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>




UPDATED!!!! Yes, profs you are right, in my case it is the easiest way to put controls in HeaderTemplate. I forgot about it. But I have a little question, how can I add names of columns without adding additional Labels? Here is my code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" DataKeyNames="username" DataSourceID="SqlDataSource1" 
        ForeColor="#333333" GridLines="None" onrowcreated="GridView1_RowCreated">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:TemplateField **HeaderText="username"** SortExpression="username">
            <HeaderTemplate>
                **<asp:Label ID="Label4" runat="server" Text="username"></asp:Label>**<br />
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name" SortExpression="Name">
            <HeaderTemplate>
            <asp:Label ID="Label4" runat="server" Text="Name"></asp:Label><br />
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Surname" SortExpression="Surname">
            <HeaderTemplate>
            <asp:Label ID="Label4" runat="server" Text="Surname"></asp:Label><br />
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Surname") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>

I marked bold strings. If I leave without Label (just with HeaderText="username") it isn't show me any name in header column. Only if I add Labels it shows my names. What is wrong with it?

Dynamically you would have to add a second header row after every has been databound

Example in VB but you should be able to convert it easily enough.

Private Sub GridView1_DataBound(sender As Object, e As EventArgs) Handles GridView1.DataBound

    // Add checks for row count.

    // create a new row
    Dim gvr As New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)

    Dim gvc As TableCell

    // Create a new empty cell
    gvc = New TableCell()

    //add a new TextBox to the cell
    gvc.Controls.Add(New TextBox())

    // Add the cell to the row
    gvr.Cells.Add(gvc)

    // repeat above as necessary

    // Add row to Gridview at index 1 (0 is bound header)
    // GridView1.Controls(0) is the Gridview table
    GridView1.Controls(0).Controls.AddAt(1, gvr)
End Sub

The simplest solution is to not attempt to do it in the code behind. Instead utilize the HeaderTemplate for your TemplateFields.

Here is one as an example:

<asp:TemplateField HeaderText="username" SortExpression="username">
    <HeaderTemplate>                              
      username
      <br />
      <asp:TextBox ID="username" runat="server" />
    </HeaderTemplate>   
    <ItemTemplate...   
</asp:TemplateField>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM