内联编辑和更新网格视图数据并将其保存到实体模型数据库的方法
本文关键字:保存 实体模型 数据库 方法 编辑 更新 网格 数据 视图 | 更新日期: 2023-09-27 18:10:42
我有这个gridview代码内联编辑和更新数据到实体。我可以编辑内联内容,但不能更新它。我仍然没有得到的问题,为什么它不采取一个文本框包含字符串变量。当我调试它时,它会显示gridview中已经可用的值。
<asp:GridView ID="grdProductInfo" runat="server"
AutoGenerateColumns="False"
OnRowDeleting="grdProductInfo_RowDeleting"
OnRowEditing="grdProductInfo_RowEditing"
OnRowCancelingEdit="grdProductInfo_RowCancelingEdit"
OnRowDataBound="grdProductInfo_RowDataBound"
OnRowUpdating="grdProductInfo_RowUpdating" CellPadding="4" ForeColor="#333333" GridLines="None" DataKeyNames="ProductID" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Name" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("ProductName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle Width="150px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category ID" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblcatId" runat="server" Text='<%# Eval("CategoryID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtcatId" runat="server" Text='<%# Eval("CategoryID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity Per Unit" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblqpu" runat="server" Text='<%# Eval("QuantityPerUnit") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtqpu" runat="server" Text='<%# Eval("QuantityPerUnit") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblup" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtup" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150">
</asp:CommandField>
</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>
cs代码:
protected void grdProductInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = grdProductInfo.Rows[e.RowIndex];
string pname = (row.FindControl("txtName") as TextBox).Text;
string catId = (row.FindControl("txtcatId") as TextBox).Text;
string qpu = (row.FindControl("txtqpu") as TextBox).Text;
string up = (row.FindControl("txtup") as TextBox).Text;
using (LaunderDBEntities context = new LaunderDBEntities())
{
int productID = Convert.ToInt32(grdProductInfo.DataKeys[e.RowIndex].Value);
Product obj = context.Products.First(x => x.ProductID == productID);
obj.ProductName = pname;
obj.CategoryID = Convert.ToInt32(catId);
obj.QuantityPerUnit = Convert.ToInt32(qpu);
obj.UnitPrice = Convert.ToInt32(up);
context.SaveChanges();
grdProductInfo.EditIndex = -1;
this.DataBind();
}
}
是否在Page_Load中绑定GridView的数据?如果是,它在if(!isPostback)语句中吗?我的意思是,也许这是一个事件的问题,如果你的数据加载之前,你更新(如每次回发),它会擦除新的值,然后你会更新与旧值。