2016-06-01 86 views
0

我有一個viewproducts頁面,我想要的是當點擊編輯鏈接時,兩個字段將被編輯(名稱和價格)。這裏是我的html代碼:更新綁定字段中的數據(輸入字符串格式不正確)

<ItemTemplate> 
     <asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" CommandName="Edit" /> 
    </ItemTemplate> 
    <EditItemTemplate> 
     <asp:LinkButton ID="LinkButton2" Text="Update" runat="server" OnClick="OnUpdate" /> 
     <asp:LinkButton ID="LinkButton3" Text="Cancel" runat="server" OnClick="OnCancel" /> 
    </EditItemTemplate> 
</asp:TemplateField> 

和後面的代碼:

private void GetProducts(int CategoryID) 
    { 
     ShoppingCart k = new ShoppingCart() 
     { 
      CategoryID = CategoryID 
     }; 
     gdview.DataSource = null; 
     gdview.DataSource = k.GetAllProducts(); 
     gdview.DataBind(); 
    } 
    protected void OnRowEditing(object sender, GridViewEditEventArgs e) 
    { 
     gdview.EditIndex = e.NewEditIndex; 
     this.GetProducts(0); 
    } 
    protected void OnUpdate(object sender, EventArgs e) 
    { 
     GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow; 
     string Name = (row.Cells[0].Controls[0] as TextBox).Text; 
     string Price = (row.Cells[2].Controls[0] as TextBox).Text; 
     DataTable dt = ViewState["dt"] as DataTable; 
     dt.Rows[row.RowIndex]["Name"] = Name; 
     dt.Rows[row.RowIndex]["Price"] = Price; 
     ViewState["dt"] = dt; 
     gdview.EditIndex = -1; 
     this.GetProducts(0); 
    } 

    protected void OnCancel(object sender, EventArgs e) 
    { 
     gdview.EditIndex = -1; 
     this.GetProducts(0); 
    } 
    protected void gdview_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 

      if (Convert.ToInt32(e.Row.Cells[6].Text) < 50) 
      { 
       e.Row.Cells[0].BackColor = System.Drawing.Color.Red; 
       e.Row.Cells[0].ForeColor = System.Drawing.Color.White; 
       e.Row.Cells[6].BackColor = System.Drawing.Color.Red; 
       e.Row.Cells[6].ForeColor = System.Drawing.Color.White; 
      } 
     } 
    } 

但是它給了我一個錯誤說:輸入字符串的不正確的格式。和紅色文字是:

if (Convert.ToInt32(e.Row.Cells[6].Text) < 50) 

我在這裏錯過了什麼?

這裏是GridView的代碼:當編輯文本框是可見

<Columns> 


     <asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name" > 
      <ItemStyle Height="20px" Width="150px" /> 
     </asp:BoundField> 

     <asp:BoundField HeaderText="ProductCategory " ReadOnly="true" DataField="CategoryName" SortExpression="CategoryNaame" > 
      <ItemStyle Height="20px" Width="150px" /> 
     </asp:BoundField> 

     <asp:BoundField HeaderText="Price" DataField="Price" SortExpression="Price" > 
      <ItemStyle Height="20px" Width="150px" /> 
     </asp:BoundField> 

     <asp:ImageField HeaderText ="ImageUrl" DataImageUrlField="ImageUrl" SortExpression="ImageUrl" ReadOnly="true" ControlStyle-Width ="10"> 

     <ControlStyle Width="50px"></ControlStyle> 

     </asp:ImageField> 

     <asp:BoundField HeaderText="ProductQuantity" DataField="ProductQuantity" ReadOnly="true" SortExpression="ProductQuantity" > 
      <ItemStyle Height="20px" Width="150px" /> 
     </asp:BoundField> 

     <asp:BoundField HeaderText="ProductSold" DataField="ProductSold" SortExpression="ProductSold" ReadOnly="true" > 
      <ItemStyle Height="20px" Width="150px" /> 
     </asp:BoundField> 

     <asp:BoundField HeaderText="AvailableStock" DataField="AvailableStock" SortExpression="AvailableStock " ReadOnly="true" > 
      <ItemStyle Height="20px" Width="150px" /> 
     </asp:BoundField> 

名稱字段中消失?

here is the picture

+0

'e.Row.Cells [6] .Text'的值是什麼? – Mairaj

+0

@MairajAhmad現貨。 INT。它變得紅色,當它達到不到20 –

+0

我不這麼認爲,當拋出錯誤時,這一定是空的。把這個放在你的條件之前if(!string.IsNullOrEmpty(e.Row.Cells [6] .Text))' – Mairaj

回答

0

細胞的文本爲空,不能轉化爲int這樣就把檢查,如果在沒有空的文本不是將其轉換爲int

if(!string.IsNullOrEmpty(e.Row.Cells[6].Text)) 

如果你不想使列可編輯設置屬性ReadOnly="true"

ReadOnly="true" 

例如,如果你想要se t coulmn ReadOnly="true"

<asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name" ReadOnly="true"> 
    <ItemStyle Height="20px" Width="150px" /> 
</asp:BoundField> 
+0

最後一個問題先生。當我點擊更新鏈接時,名稱列消失了?請看我編輯的問題,謝謝:) –

+0

確保它沒有設置爲只讀。 – Mairaj

+0

是的,它不是先生。與價格欄不同,價值仍然存在。但是帶有名稱,當編輯文本框出現時它不可見。 –