如题,GridView某列数据都是23% 56% 这些百分比的数,现在要求平均百分比,貌似很多人都解决不了,谁有高见吗?如果可以忽略%号的话,直接求数字的平均值,显示的时候加%号就好了,但是这样,要怎么操作,GridView里才能显示%号,但是实际求平均的时候,%不参与其中的运算呢?可不可以把该列转换成模板列,在里面加什么控件可以固定显示%号,求值不会参与运算呢?
我的求平均百分比代码:
if (e.Row.RowIndex >= 0)
{
if (e.Row.Cells[4].Text != "")
{
s += Convert.ToString()(e.Row.Cells[4].Text);
}
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "平均百分比:" + ((double)s/(double)GridView1.Rows.Count).ToString();但是运行之后只会显示 平均百分比:0
我的求平均百分比代码:
if (e.Row.RowIndex >= 0)
{
if (e.Row.Cells[4].Text != "")
{
s += Convert.ToString()(e.Row.Cells[4].Text);
}
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "平均百分比:" + ((double)s/(double)GridView1.Rows.Count).ToString();但是运行之后只会显示 平均百分比:0
{
if (e.Row.Cells[4].Text != "")
{
s += Convert.ToString()(e.Row.Cells[4].Text.Replace("%",""));
}
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[4].Text = "平均百分比:" + ((double)s/(double)GridView1.Rows.Count).ToString()+"%";
试试吧
然后再GridView的模板列中 用DataFromt() 格式 {0:n2}%,n2是取2位小数
编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误消息: CS0120: 非静态字段、方法或属性“object.ToString()”要求对象引用源错误: 行 103: if (e.Row.Cells[4].Text != "")
行 104: {
行 105: s += Convert.ToString()(e.Row.Cells[10].Text.Replace("%", ""));行 106: }
行 107: }
改成
s += (e.Row.Cells[4].Text.Replace("%", "")).ToString();