使用Gridview页脚添加记录

本文关键字:添加 加记录 Gridview 使用 | 更新日期: 2023-09-27 18:04:48

我正在使用我的gridview的页脚添加一个新记录,虽然我不喜欢格式。我想能够有我的保存/取消按钮在一行的编辑/删除命令字段。我想不出该怎么做。有人能帮忙吗?欢呼声

<asp:GridView ID="Grd" runat="server" AutoGenerateColumns="false" OnRowEditing="Grd_RowEditing"
                                        OnRowCancelingEdit="Grd_RowCancelingEdit" OnRowUpdating="Grd_RowUpdating"
                                        Width="600px" CssClass="HomeDG" SelectedRowStyle-CssClass="HomeDGSelected" RowStyle-CssClass="HomeDGItem"
                                        AlternatingRowStyle-CssClass="HomeDGAlternating" HeaderStyle-CssClass="HomeDGhead"
                                        PageSize="10">
                                            <Columns>
                                                <asp:TemplateField HeaderText="Employee">
                                                    <ItemTemplate>
                                                        <asp:HiddenField ID="hfID" runat="server" Value='<%# Bind("Emp_ID")%>' />
                                                        <asp:Label ID="LblEmployee" runat="server" Text='<%# Bind("Emp_Name")%>'></asp:Label>
                                                    </ItemTemplate>
                                                    <FooterTemplate>
                                                        <asp:DropDownList ID="ddlNewEmp" runat="Server" />
                                                    </FooterTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Start Date">
                                                    <EditItemTemplate>
                                                        <asp:TextBox ID="TxtStartDate" runat="server" 
                                                        Text='<%#  Eval("Start_Date","{0:dd-MMM-yyyy}") %>'></asp:TextBox>
                                                       </EditItemTemplate>
                                                        <FooterTemplate>
                                                        <asp:TextBox ID="TxtStartDate" runat="Server" />
                                                        <asp:CalendarExtender ID="calStartDate" runat="server" TargetControlID="TxtStartDate"
                                                            Format="dd-MMM-yyyy" />
                                                        <asp:Button ID="btnAddNewSave" runat="server" Text="Save" CausesValidation="False"
                                                            OnClick="btnAddNewSave_Click" />
                                                        <asp:Button ID="btnAddNewCancel" runat="server" Text="Cancel" OnClick="btnAddNewCancel_Click" />
                                                    </FooterTemplate>
                                                </asp:TemplateField>
                                                <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" CausesValidation="False" />
                                            </Columns>
                                        </asp:GridView>
                                        <asp:Button ID="btnAddNew" runat="server" Text="Add" CausesValidation="False" OnClick="btnAddNew_Click" />

使用Gridview页脚添加记录

为grid添加RowCreated事件,并在该处理程序中检查

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            var but = new LinkButton();
            but.Text = "Save";
            e.Row.Cells[0].Controls.Add(but);
        }