2011-11-03 141 views
0
protected void CustomersGridView_DataBound(Object sender, EventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      decimal rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "DC_No_Decimal")); 
      if (e.Row.DataItemIndex >= inderGrid.PageIndex * inderGrid.PageSize && e.Row.DataItemIndex < inderGrid.PageIndex * inderGrid.PageSize + inderGrid.PageSize) 
      { 
       grdTotal = grdTotal + rowTotal; 
      } 
     } 
     else if (e.Row.RowType == DataControlRowType.Footer && inderGrid.PageCount == inderGrid.PageIndex + 1) 
     { 
      for (int i = 0; i < inderGrid.Rows.Count; i++) 
      { 
       decimal currentRowCellVal = Convert.ToDecimal(inderGrid.Rows[i].Cells[3].Text); 
       grdTotal += currentRowCellVal; 
      } 
      e.Row.Cells[3].Text = grdTotal.ToString("c"); 
     } 
     else if (e.Row.RowType == DataControlRowType.Footer) 
     {  
       e.Row.Cells[0].Text = "Totals:"; 
       // for the Footer, display the running totals 
       e.Row.Cells[3].Text = grdTotal.ToString("0.00"); 
       // e.Row.Cells[2].Text = quantityTotal.ToString("d");   
       e.Row.Cells[1].HorizontalAlign = e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right; 
       e.Row.Font.Bold = true; 
     } 
    } 

此網格的目的是在網格的最終頁面中顯示總數。當我要去頁頁我得到一個錯誤:輸入字符串格式不正確

Input string was not in a correct format.

在網格的最後一頁,我需要總所有頁面的細胞的電網[3]

+4

你的問題是不是在[正確的格式(http://tinyurl.com/so-hints)。閉幕......啊,順便問一下,你的問題是什麼? –

+0

嗨拉傑,你可以更新你的問題,給我們更多的信息,問題在哪裏,至少要問一個問題,不要只是把標題。 – Purplegoldfish

+1

你能告訴我們拋出異常的行嗎?或者如果你可以用提到的代碼附上異常屏幕截圖? –

回答

1

你說你的錯誤是:

e.Row.Cells[3].Text = grdTotal.ToString("0.00"); 

你必須使用一個格式化:

e.Row.Cells[3].Text = String.Format("{0:C}", grdTotal); 

e.Row.Cells[3].Text = grdTotal.ToString("C", CultureInfo.CurrentCulture); 

檢查grdTotal是一個十進制...

看看:MSDN

+0

我在這個地方得到錯誤decimal currentRowCellVal = Convert.ToDecimal(inderGrid.Rows [i] .Cells [3] .Text); –

+0

inderGrid.Rows [i] .Cells [3] .Text是什麼de''value''? – Peter

+0

grdTotal也用十進制表示。 –

0

我覺得你的問題是此行

e.Row.Cells[3].Text = grdTotal.ToString("0.00"); 

我假設你grdTotal變量在這裏是一個小數,使用「0.00」不是一個有效的格式化程序。

退房MSDN網站這裏http://msdn.microsoft.com/en-us/library/system.decimal.tostring.aspx

至少,你可以嘗試e.Row.Cells[3].Text = grdTotal.ToString();

+0

我在這個地方得到錯誤decimal currentRowCellVal = Convert.ToDecimal(inderGrid.Rows [i] .Cells [3] .Text); –

+0

你知道你想要轉換成十進制的值是什麼嗎?此外,我不記得如果這將工作,如果您嘗試轉換的值爲空或空字符串,或只是有一些字母。查看decimal.tryparse然後只分配值,如果解析是成功的 – Purplegoldfish

+0

單元格[3]包含數字值即金額字段 –