2016-06-01 22 views
0

私はviewproductsページを持っています。編集リンクをクリックすると、2つのフィールドが編集されます(名前と価格)。背後boundfieldのデータを更新しています(入力文字列の形式が正しくありません)

<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; 
      } 
     } 
    } 

ここに私のhtmlコードです。赤いテキストは次のとおりです。

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

ここでは何が欠けていますか?編集のためのテキストボックスが表示されているとき

<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> 

名フィールド消える:ここ

は、GridViewのコードはありますか?

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

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

に変換するよりも、空ではないにチェックを入れintに変換することができない空でありますあなたがSEにしたい場合は、列の編集可能なセットたとえばプロパティReadOnly="true"

ReadOnly="true" 

作りますこのcoulmnトンReadOnly="true"

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

最後の質問です。更新リンクをクリックすると、名前列が消えますか?私の編集した質問をご覧くださいありがとうございます:) –

+0

読み取り専用に設定されていないことを確認してください。 – Mairaj

+0

はいそうです。価格列とは異なり、価値はまだそこにあります。名前を使用すると、テキストボックスの編集が表示されても表示されません。 –

関連する問題