开发者

Adding a New Row in GridView

The below code work just fine but my question is:

When i click on add new: instead of showing at the footer, is there a way i can show at the top of开发者_JAVA技巧 all rows(like in header)? If i have 15 rows, the new blank row will be all the way at the end, i want to make it easy on user and show at the first row.

<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>


You could show the add-row in Header and/or Footer, have a look at RowCreated-Event.

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>

Codebehind:

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

Note: this sample will add a "add-row" to the HeaderRow(at the top) and the FooterRow(remove the FooterTemplate if desired). If you want to add it below the autogenerated Header-Columns, i'll have to have a further look at it. Because the Header does not exist at this time in RowCreated, i had the problem to add my new row below it.


Have you tried putting the FooterTemplate markup above in the HeaderTemplate? That should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜