简体   繁体   English

将新行添加到GridView的Header中

[英]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. 我有一个问题,添加一个新行与几个控件,如textBoxes到GridView的Header。 When I add them into Header in GridView_RowCreated method with ... 当我将它们添加到GridView_RowCreated方法中的Header中时...

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. 我希望我创建的行放在第二个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. 我试图自己做,首先,我将ShowHeader修改为false并以编程方式添加第一行和第二行,但这是错误的方式,特别是当网格没有要显示的数据但是有必要显示标题时。 I found this discussion similar discussion , but intellisence doesn't know about override PrepareControlHierarchy. 我发现这个讨论类似的讨论 ,但intellisence不知道有关覆盖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. 我试图用Object浏览器搜索它,发现这是GridView的方法,但是我无法插入它并进行测试。 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. 是的,教授你是对的,在我看来,这是将控件放在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. 如果我不使用Label(只使用HeaderText =“username”),则不会在标题栏中显示任何名称。 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. VB中的示例,但您应该能够轻松地转换它。

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. 而是使用HeaderTemplate作为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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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