在GridView中添加新行

时间:2011-07-13 18:54:38

标签: asp.net gridview

以下代码工作正常,但我的问题是:

当我点击添加新内容:而不是在页脚显示时,是否有一种方法可以显示在所有行的顶部(如标题中)?如果我有15行,那么新的空白行将一直到最后,我想让用户轻松一下并在第一行显示。

<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" AllowSorting="True" AutoGenerateColumns="False" ShowFooter="True" OnRowCommand="GridView1_RowCommand">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<RowStyle BackColor="White" ForeColor="#003399" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<Columns>
<asp:BoundField DataField="PersonID" HeaderText="PersonID" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" Text='<%# Eval("Name") %>' runat="server"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="Button1" CommandName = "ADD" runat="server"
Text="Add" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

2 个答案:

答案 0 :(得分:2)

您可以在页眉和/或页脚中显示添加行,查看RowCreated - 活动。

ASPX:

<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC"
    BorderStyle="None" BorderWidth="1px" CellPadding="4" AllowSorting="True" AutoGenerateColumns="False"
    ShowFooter="True" OnRowCommand="GridView1_RowCommand" OnRowCreated="GridView1_RowCreated">
    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
    <RowStyle BackColor="White" ForeColor="#003399" />
    <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
    <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
    <Columns>
        <asp:BoundField DataField="PersonID" HeaderText="PersonID" />
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="Label1" Text='<%# Eval("Name") %>' runat="server"></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                <asp:Button ID="BtnAdd" CommandName="ADD" runat="server" Text="Add" />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码隐藏:

Public Class GridViewAddRowTop
    Inherits System.Web.UI.Page

    Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Header Then
            Dim extraRow As New GridViewRow(1, -1, DataControlRowType.Header, DataControlRowState.Normal)
            Dim emptyCell As New TableCell()
            Dim tc As TableCell = New DataControlFieldCell(DirectCast(e.Row.Cells(0), DataControlFieldCell).ContainingField)
            Dim BtnAdd As New Button()
            Dim TxtName As New TextBox
            emptyCell.Controls.Add(New LiteralControl("&nbsp;"))
            extraRow.Cells.Add(emptyCell)
            TxtName.ID = "TxtName"
            tc.Controls.Add(TxtName)
            BtnAdd.ID = "BtnAdd"
            BtnAdd.CommandName = "ADD"
            BtnAdd.Text = "Add"
            tc.Controls.Add(BtnAdd)
            extraRow.Cells.Add(tc)
            DirectCast(sender, GridView).Controls(0).Controls.Add(extraRow)
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim tbl = getDataSource()
            Me.GridView1.DataSource = tbl
            Me.GridView1.DataBind()
        End If
    End Sub

    Private Function getDataSource() As DataTable
        Dim tbl = New DataTable()
        Dim idColumn = New DataColumn("PersonID", GetType(Int32))
        idColumn.AutoIncrement = True
        tbl.Columns.Add(idColumn)
        tbl.Columns.Add(New DataColumn("Name", GetType(String)))
        For i As Int32 = 1 To 15
            Dim newRow = tbl.NewRow
            newRow("Name") = i & ".Name"
            tbl.Rows.Add(newRow)
        Next
        Return tbl
    End Function

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
        If e.CommandName.ToUpper = "ADD" Then
            Dim tbl = getDataSource()
            Dim newRow = tbl.NewRow
            Dim bu As New Button
            Dim txtName = DirectCast(DirectCast(e.CommandSource, WebControl).NamingContainer.FindControl("txtName"), TextBox)
            newRow("Name") = txtName.Text
            tbl.Rows.Add(newRow)
            DirectCast(sender, GridView).DataSource = tbl
            DirectCast(sender, GridView).DataBind()
        End If
    End Sub

End Class

注意:此示例将向HeaderRow(在顶部)和FooterRow(如果需要,删除FooterTemplate)添加“添加行”。如果你想将它添加到自动生成的Header-Columns下面,我将不得不进一步查看它。因为在RowCreated中此时Header不存在,所以我有问题在它下面添加我的新行。

答案 1 :(得分:0)

您是否尝试将FooterTemplate标记放在HeaderTemplate中?这应该有用。