2015-05-26 21 views
1

我想問一下,當我在文本框中鍵入值時,如何比較「可兌換數量」和「可用數量」?如何比較列表與gridview中的文本框

例如,在我的gridview中,我在文本框中鍵入「2」,但可用數量只有「1」。所以我希望顯示錯誤顯示數量不夠。 我的驗證只會做在GridView的最後一行

(我爲我凌亂的編碼技術道歉,我無法發佈圖像,由於低信譽,將在評論截圖)

我的按鈕,點擊

Dim row As GridViewRow 
    Dim strAvailable As String 
    Dim strRedeem As String 

    For Each row In GridView1.Rows 
     ' write ur DB process code here 
     strAvailable = CType(row.Cells(4).FindControl("lblAvailable"),Label).Text 
     strRedeem = CType(row.Cells(5).FindControl("txtRedeem"), TextBox).Text 

     If strRedeem > strAvailable Then 
      lblMessage.Text = "Quantity not enough" 
      btnPrint.Visible = False 

     Else 
      lblMessage.Text = "Enough Quantity" 
      btnPrint.Visible = True 
     End If 
    Next 
end sub 
+0

什麼代碼給出錯誤「輸入字符串格式不正確」。當您嘗試將某種類型解析爲其他類型時,通常會出現此錯誤。 – Mairaj

+0

http://postimg.org/image/6tsi1dxzn/ 這是錯誤,當我嘗試添加「測試」 – Trya

+0

好吧,你需要像這樣寫lblMessage.Text =「Total number:」+ totalA.ToString() ; – Mairaj

回答

0

你需要指定值時,文本框,因爲你是串聯string and int並將其分配給文本框或標籤的Text屬性,它需要一個string調用ToString()方法。

lblMessage.Text = "Total number: " + totalA.ToString(); 

您比較two strings但你需要比較兩個int variables,所以你需要轉換string to int

Dim row As GridViewRow 
Dim strAvailable As Integer 
Dim strRedeem As Integer 

For Each row In GridView1.Rows 
    //write ur DB process code here 
    strAvailable = CType(CType(row.Cells(4).FindControl("lblAvailable"),Label).Text,int); 
    strRedeem =CType(CType(row.Cells(5).FindControl("txtRedeem"), TextBox).Text,int); 

    If strRedeem > strAvailable Then 
     lblMessage.Text = "Quantity not enough" 
     btnPrint.Visible = False 

    Else 
     lblMessage.Text = "Enough Quantity" 
     btnPrint.Visible = True 
    End If 
Next 

末次

注意我不知道VB的確切語法,所以請原諒我這一點,並糾正它,如果它是不正確的。