如何动态编辑网格视图数据

本文关键字:编辑 网格 视图 数据 动态 何动态 | 更新日期: 2023-09-27 18:32:34

如何编辑和删除网格视图中的字段,未与数据库连接,并且网格视图正在使用用户在文本框中输入的数据填充?另外,如何使用网格视图中的删除按钮删除行?

这是我在ASP页面中的网格视图:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="mGrid"
        ShowFooter="True" ShowHeaderWhenEmpty="True" Width="70%" OnRowDataBound="GridView1_RowDataBound"
        OnRowCommand="GridView1_RowCommand" OnRowCancelingEdit="GridView1_RowCancelingEdit"
        OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
    <AlternatingRowStyle CssClass="alt" />
    <Columns>
        <asp:TemplateField HeaderText="Item Code">
            <EditItemTemplate>
                <asp:TextBox ID="txtItemCode" runat="server" Text='<%# Bind("ItemCode") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="ItemCode" runat="server" Text='<%# Bind("ItemCode") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Product">
            <EditItemTemplate>
                <asp:TextBox ID="txtProduct" runat="server" Text='<%# Bind("Product") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Product" runat="server" Text='<%# Bind("Product") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Unit Price">
            <EditItemTemplate>
                <asp:TextBox ID="txtUnitPrice" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="UnitPrice" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Batch">
            <EditItemTemplate>
                <asp:TextBox ID="txtBatch" runat="server" Text='<%# Bind("Batch") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Batch" runat="server" Text='<%# Bind("Batch") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField FooterText="Total" HeaderText="Expiary Date">
            <EditItemTemplate>
                <asp:TextBox ID="txtExpiaryDate" runat="server" Text='<%# Bind("ExpiaryDate") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="ExpiaryDate" runat="server" Text='<%# Bind("ExpiaryDate") %>'></asp:Label>
            </ItemTemplate>
            <FooterStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Quantity">
            <EditItemTemplate>
                <asp:TextBox ID="txtQuantity" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Quantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="GetQuantity" runat="server" Text='<%# GetQuantity() %>'></asp:Label>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label ID="Total" runat="server" Text='<%# Eval("Total") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="GetTotal" runat="server" Text='<%# GetTotal() %>'></asp:Label>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/delete.png" EditImageUrl="~/Images/edit.png"
                HeaderText="EDIT" ShowDeleteButton="True" ShowEditButton="True" />
    </Columns>
    <FooterStyle BackColor="Gainsboro" ForeColor="Black" />
    <PagerStyle CssClass="pgr" HorizontalAlign="Right" /> 
 </asp:GridView>

我使用此代码将数据添加到网格。

protected void btnAdd_Click(object sender, EventArgs e)
{
    if (ViewState["CurrentData"] != null)
    {
        var dt = (DataTable)ViewState["CurrentData"];
        var count = dt.Rows.Count;
        BindGrid(count);
    }
    else
    {
        BindGrid(1);
    }
    Clear();
}
private void BindGrid(int rowcount)
{
    var DataTbl = new DataTable();
    DataRow DataRw;
    DataTbl.Columns.Add("ItemCode", typeof(String));
    DataTbl.Columns.Add("Product", typeof(String));
    DataTbl.Columns.Add("UnitPrice", typeof(String));
    DataTbl.Columns.Add("Batch", typeof(String));
    DataTbl.Columns.Add("ExpiaryDate", typeof(String));
    DataTbl.Columns.Add("Quantity", typeof(double));
    DataTbl.Columns.Add("Total", typeof(double));
    if (ViewState["CurrentData"] != null)
    {
        for (var i = 0; i < rowcount + 1; i++)
        {
            DataTbl = (DataTable)ViewState["CurrentData"];
            if (DataTbl.Rows.Count > 0)
            {
                DataRw = DataTbl.NewRow();
                DataRw[0] = DataTbl.Rows[0][0].ToString();
            }
        }
        DataRw = DataTbl.NewRow();
        DataRw[0] = txtItemCode.Text;
        DataRw[1] = ddlProduct.SelectedItem;
        DataRw[2] = txtUnitPrice.Text;
        DataRw[3] = txtBatch.Text;
        DataRw[4] = txtExpiaryDate.Text;
        DataRw[5] = txtQty.Text;
        DataRw[6] = (Convert.ToInt32(Convert.ToDouble(
            txtUnitPrice.Text) * Convert.ToDouble(
                txtQty.Text)) - ((Convert.ToDouble(
                    txtUnitPrice.Text) * Convert.ToDouble(
                        txtQty.Text))) * Convert.ToDouble(
                            txtProductDiscount.Text) / 100).ToString();
        DataTbl.Rows.Add(DataRw);
    }
    else
    {
        DataRw = DataTbl.NewRow();
        DataRw[0] = txtItemCode.Text;
        DataRw[1] = ddlProduct.SelectedItem;
        DataRw[2] = txtUnitPrice.Text;
        DataRw[3] = txtBatch.Text;
        DataRw[4] = txtExpiaryDate.Text;
        DataRw[5] = txtQty.Text;
        DataRw[6] = (Convert.ToInt32(Convert.ToDouble(
            txtUnitPrice.Text) * Convert.ToDouble(
                txtQty.Text)) - ((Convert.ToDouble(
                    txtUnitPrice.Text) * Convert.ToDouble(
                        txtQty.Text))) * Convert.ToDouble(
                            txtProductDiscount.Text) / 100).ToString();
        DataTbl.Rows.Add(DataRw);
    }
    if (ViewState["CurrentData"] != null)
    {
        GridView1.DataSource = (DataTable)ViewState["CurrentData"];
        GridView1.DataBind();
    }
    else
    {
        GridView1.DataSource = DataTbl;
        GridView1.DataBind();
    }
    ViewState["CurrentData"] = DataTbl;
}

如何动态编辑网格视图数据

首先,您的代码和查询不清楚。如果我理解正确,那么您代码中的问题是数据没有删除或更新。

正如我在您的代码中看到的那样,您将数据保存在视图状态中。 请在GridView1_RowUpdating()GridView1_RowCommand()(希望您将其用于删除)事件阶段重新检查您的代码。 deleting/updating数据后,还应更新视图状态。