2016-06-01 9 views
0

viewproducts 페이지가 있는데 편집 링크를 클릭하면 두 필드가 편집됩니다 (이름 및 가격). 뒤에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

에서 t? – 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로 변환 할 수있는 비어 당신이 원한다면 예를 들어 열 편집 세트에게 재산 ReadOnly="true"

ReadOnly="true" 

를 만들기 위해 자체 이 coulmn`e.Row.Cells [6] .Text`의 값이 무엇 ReadOnly="true"

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

마지막 질문. 업데이트 링크를 클릭하면 이름 열이 사라집니다. 제 편집 된 질문을 보시고 고맙습니다 :) –

+0

읽기 전용으로 설정되지 않았는지 확인하십시오. – Mairaj

+0

예 그 아닙니다. 가격 열과 달리 값은 여전히 ​​있습니다. 편집 텍스트 상자가 나타나면 이름이 보이지 않습니다. –