2011-11-03 169 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]包含数字值即金额字段 –