2014-01-16 7 views
1

我試圖在link中做計算,但它說輸入字符串的格式不正確。我正在gridview中計算單元格的值。 這裏是我的代碼:輸入字符串在格單元格的gridview計算中的格式不正確

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    int tot = 0; 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
      decimal medjtot = (Convert.ToInt32(e.Row.Cells[3].Text) * (Convert.ToDecimal(e.Row.Cells[4].Text)/12)) * Convert.ToDecimal(e.Row.Cells[5].Text); 
      Label RowTotal = (Label)e.Row.FindControl("Label1"); 
      RowTotal.Text = medjtot.ToString();  
    } 
} 
+0

在哪條線上? e.Row.Cells [3] .Text','e.Row.Cells [4] .Text'和'e.Row.Cells [5] .Text'的值究竟是什麼? –

+0

這些行是文本框。我將計算我將在那輸入的值。 – jelliaes

+0

不知道這些值是不可能的。請提供它們.. –

回答

1

確保您在Text屬性每個Row Cell做保護檢查。文本可能是whitespaceempty

樣本衛兵檢查。

if(string.IsNullOrWhiteSpace(e.Row.Cells[5].Text) || 
     string.IsNullOrWhiteSpace(e.Row.Cells[3].Text)) 
      return; 
+0

感謝您的迴應:)是否有任何其他方式來計算最後一行的gridview。因爲我的代碼不起作用。 – jelliaes

0

必須是該行:

decimal medjtot = (Convert.ToInt32(e.Row.Cells[3].Text) * (Convert.ToDecimal(e.Row.Cells[4].Text)/12)) * Convert.ToDecimal(e.Row.Cells[5].Text); 

如果你的細胞含有禁伐字符(字母爲例如Int32)將轉換將失敗。檢查文化,小數分隔符可能是昏迷或點。

+0

你是對的。錯誤是那條線。但我不知道爲什麼它是錯的。這些單元格只是沒有任何價值的文本框。 – jelliaes

+0

我的意思是我沒有輸入任何東西。當我運行我的程序時,它顯示錯誤。 – jelliaes

+1

所以看看第二個答案。你不能使用空字符串轉換 – Plue