在网格视图中使用按钮添加空白行

时间:2017-02-03 02:17:32

标签: c# asp.net gridview code-behind bulkinsert

我有关于页脚和addbutton的gridview,如果我按下addbutton,如何添加新行?所以我可以输入新的数据。我也希望将它们全部保存到数据库中,有人可以解释一下

这是我的正面代码:

<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" CellPadding="4" ForeColor="#333333"   
                        GridLines="None" ShowFooter="True" Width="707px" >  
                    <AlternatingRowStyle BackColor="White"/>  
                    <Columns>  
                        <asp:TemplateField>
                            <FooterTemplate>
                                <asp:Button ID="AddProduct" runat="server" CssClass="button" Text="Add" onclick="AddProduct_Click" ></asp:Button>
                            </FooterTemplate>
                        </asp:TemplateField>
                    <asp:BoundField  HeaderStyle-Width="120px" HeaderText="Id Trans" DataField="Id_Trans" ShowHeader="False" Visible="False">    
                        <HeaderStyle Width="120px"></HeaderStyle>
                        </asp:BoundField>
                        <asp:TemplateField HeaderText="Nama Barang" SortExpression="Nama_Item">
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="tampilbarang" DataTextField="Nama_Item" DataValueField="Id_Item">
                                </asp:DropDownList>
                                <asp:SqlDataSource ID="tampilbarang" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Item]"></asp:SqlDataSource>
                            </ItemTemplate>
                            <HeaderStyle Width="120px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="QTY">
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                            </ItemTemplate>
                            <HeaderStyle Width="120px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Keterangan Penggunaan">
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                            </ItemTemplate>
                            <HeaderStyle Width="120px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Aksi">
                            <ItemTemplate>
                                <asp:Button ID="Del" runat="server" Height="22px" Text="Delete" Width="57px" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>  
                        <EditRowStyle BackColor="#2461BF" />  
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />  
                        <RowStyle BackColor="#EFF3FB" />  
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />  
                        <SortedAscendingCellStyle BackColor="#F5F7FB" />  
                        <SortedAscendingHeaderStyle BackColor="#6D95E1" />  
                        <SortedDescendingCellStyle BackColor="#E9EBEF" />  
                        <SortedDescendingHeaderStyle BackColor="#4870BE" />  
                    </asp:GridView>

2 个答案:

答案 0 :(得分:0)

您可以在页脚模板中添加一个按钮,如下例所示:

<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
        <Columns>
        <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
        <asp:TemplateField HeaderText="Header 1">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Header 2">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Header 3">
            <ItemTemplate>
                 <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </ItemTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
             <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" />
            </FooterTemplate>
        </asp:TemplateField>
        </Columns>
</asp:gridview>

答案 1 :(得分:0)

在回答您的问题时需要解释很多事情,所以我建议您使用一个好的教程 - 试试这个 - https://quickstarts.asp.net/quickstartv20/aspnet/doc/ctrlref/data/gridview.aspx