如何编辑和删除gridview中未与数据库和gridview连接的字段正在填充用户在文本框中输入的数据?另外,如何使用gridview中的删除按钮删除行?
这是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;
}
答案 0 :(得分:0)
首先,您的代码和查询不明确。 如果我理解它,那么代码中的问题就是数据没有删除或更新。
正如我在您的代码中看到的那样,您在视图状态下保存数据。请在GridView1_RowUpdating()
和GridView1_RowCommand()
(希望您使用此删除)事件的阶段重新检查您的代码。在deleting/updating
数据之后,您还应该更新视图状态。