2015-09-01 102 views
0

我有這個Gridview代碼用於內聯編輯和更新數據到實體。我可以在線編輯內容,但無法更新它。我仍然沒有想到爲什麼它沒有將文本框包含到字符串變量中。當我調試它時,它會顯示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(); 
      } 

    } 
+0

你面對什麼確切的問題?你是否無法在後面的代碼中從網格中提取新的值?或者是數據沒有保存到數據庫?或者它不能在成功保存後在網格中刷新? –

+0

@SivaGopal它沒有從網格文本框中獲取新輸入的數據,也沒有將它更新到db –

回答

0

你綁定你的GridView的DATAS在Page_Load中?如果是的話,它在if(!isPostback)語句中?我的意思是,也許這是一個事件的問題,如果您的數據在更新之前加載(如每次回發),它將清除新值,然後用舊值更新。

+0

謝謝。它的工作原理。 –

相關問題