
用GridView显示数据,并且要可编辑和删除,在页脚插入求和值。写了求和代码之后,点编辑的时候会出错,删 100
列求和代码如下:privatedoublesum=0;protectedvoidGridView1_RowDataBound(objectsender,GridViewR...
列求和代码如下:
private double sum = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[4].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "总人数:" + sum.ToString();
}
出错信息是:输入字符串的格式不正确。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.FormatException: 输入字符串的格式不正确。
源错误:
行 27: if (e.Row.RowIndex >= 0)
行 28: {
行 29: sum += Convert.ToDouble(e.Row.Cells[4].Text);
行 30: }
行 31: else if (e.Row.RowType == DataControlRowType.Footer)
帮忙看看是哪里的问题
sum += Convert.ToInt32(e.Row.Cells[3].Text);
改成
if(e.Row.Cells[3].Text!="")
{
sum += Convert.ToInt32 (e.Row.Cells[3].Text);
}
我用这个可以了 但是如果列是百分数的话 又出错了 哪位大大看看百分数的情况下该怎么改啊 展开
private double sum = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[4].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "总人数:" + sum.ToString();
}
出错信息是:输入字符串的格式不正确。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.FormatException: 输入字符串的格式不正确。
源错误:
行 27: if (e.Row.RowIndex >= 0)
行 28: {
行 29: sum += Convert.ToDouble(e.Row.Cells[4].Text);
行 30: }
行 31: else if (e.Row.RowType == DataControlRowType.Footer)
帮忙看看是哪里的问题
sum += Convert.ToInt32(e.Row.Cells[3].Text);
改成
if(e.Row.Cells[3].Text!="")
{
sum += Convert.ToInt32 (e.Row.Cells[3].Text);
}
我用这个可以了 但是如果列是百分数的话 又出错了 哪位大大看看百分数的情况下该怎么改啊 展开
展开全部
sum += Convert.ToDouble(e.Row.Cells[4].Text);
这行 有错.
因为 .Row.Cells[4].Text 中的值有可能 为 空 或者其他非法的字符串,因此 不能强制转化为 double 类型.
最好这样:
double d=0;
if(double.tryParse(e.Row.cell[4].Text,out double d))
{
sum+=d;
}
这行 有错.
因为 .Row.Cells[4].Text 中的值有可能 为 空 或者其他非法的字符串,因此 不能强制转化为 double 类型.
最好这样:
double d=0;
if(double.tryParse(e.Row.cell[4].Text,out double d))
{
sum+=d;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
改成这样你试试
if(e.Row.Cells[3].Text!="")
{
string cellValue = e.Row.Cells[3].Text;
if (cellValue.Contains('%'))
{
cellValue = cellValue .Replace("%","");
}
sum += Convert.ToInt32 (cellValue );
}
if(e.Row.Cells[3].Text!="")
{
string cellValue = e.Row.Cells[3].Text;
if (cellValue.Contains('%'))
{
cellValue = cellValue .Replace("%","");
}
sum += Convert.ToInt32 (cellValue );
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要是百分数 的话 把结果*100 再后面加%
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询