简体   繁体   English

c#使用asp.net显示GridView页脚中的列总数

[英]c# Display sum of columns total in gridview footer using asp.net

I need display sum of Columns total ( Grand Total ) in GridView Footer in ASP.Net using C#. 我需要使用C#在ASP.Net中的GridView页脚中显示列总计( 总计 )的总和。

I have two problems: 我有两个问题:

  1. I can't apply the css style in GridView Footer ; 我不能在GridView Footer中应用CSS样式;
  2. I have error 我有错误

Specified cast is not valid. 指定的演员表无效。

when add the Sum of Tot2. 添加Tot2之和时。

How to resolve this? 如何解决呢?

Thank you advance for any help. 谢谢您的任何帮助。

This is SQL query: 这是SQL查询:

        sql = " SELECT ZN, ";
        sql += "    IFNULL(Tot1,0) AS Tot1, ";
        sql += "    IFNULL(Tot2,0) AS Tot2 ";
        sql += " FROM ";
        sql += "    doTable ";
        sql += " ORDER BY ";
        sql += "    Tot1 DESC; ";

+----+------+------+
| Zn | Tot1 | Tot2 |
+----+------+------+
| ZO |    3 |    0 |
| ZO |    3 |    0 |
| ZO |    2 |    1 |
| ZO |    2 |    0 |
| ZO |    2 |    0 |
| ZO |    2 |    0 |
| ZO |    2 |    0 |
| ZO |    1 |    0 |
| ZO |    1 |    1 |
| ZO |    1 |    0 |
+----+------+------+
10 rows in set

This the code-behind : 这是背后代码

OdbcDataAdapter adapter = new OdbcDataAdapter(command);
adapter.Fill(dsProducts);

gvProducts.Columns[1].FooterText = "Total";
gvProducts.Columns[2].ItemStyle.HorizontalAlign = HorizontalAlign.Right;
gvProducts.Columns[2].ItemStyle.CssClass = "ddl_Class_new";
gvProducts.Columns[2].FooterText = dsProducts.Tables[0].AsEnumerable().Select(x => x.Field<Int32>("Tot1")).Sum().ToString();

gvProducts.Columns[3].ItemStyle.HorizontalAlign = HorizontalAlign.Right;
gvProducts.Columns[3].ItemStyle.CssClass = "ddl_Class_new";
gvProducts.Columns[3].FooterText = dsProducts.Tables[0].AsEnumerable().Select(x => x.Field<Int32>("Tot2")).Sum().ToString();

First, you set the css of the footer with FooterStyle.CssClass 首先,使用FooterStyle.CssClass设置页脚的css

gvProducts.Columns[3].FooterStyle.CssClass = "ddl_Class_new";

And specified cast means that there are fields in Tot1 or Tot2 that are not convertible to Int32 . 并且指定的Tot2转换意味着Tot1Tot2中的Tot1字段不能转换为Int32 Probably because you use IFNULL in your query, change that to ISNULL if you use MS SQL or check if the data in aspnet is what you actually expect it to be by debugging. 可能是因为您在查询中使用了IFNULL ,所以如果您使用MS SQL,则将其更改为ISNULL ,或者通过调试检查aspnet中的数据是否是您实际期望的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM