简体   繁体   English

c#Linq数据网格视图列检查是否为空

[英]c# Linq Data grid view column checking if null

I need check if the value in GridView is null or empty using System.Linq and DataSet . 我需要使用System.LinqDataSet检查GridView中的值是否为null或为空。

I have tried this code without success because the error is : 我尝试过此代码没有成功,因为错误是:

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

How to do resolve this? 该如何解决呢?

My code below. 我的代码如下。

Thank you in advance or any help. 预先谢谢您或任何帮助。

gv.Columns[4].FooterText = ds.Tables[0].AsEnumerable()
.Select(x => x.Field<Int32>("Tot1")).Where(x => x != null).Sum().ToString();

SQL Query: SQL查询:

sql = " SELECT IFNULL(Tot1,0) AS Tot1
sql += " FROM ";
sql += "    doTable; ";

+------+
| Tot1 |
+------+
|  0   |
|  0   |
|  1   |
|  0   |
|  2   |
+------+

You have to use long for your casting, as IFNULL return BIGINT from MySQL. 您必须使用long进行转换,因为IFNULL从MySQL返回BIGINT

gv.Columns[4].FooterText = ds.Tables[0].AsEnumerable()
.Select(x => x.Field<long?>("Tot1")).Where(x => x != null).Sum().ToString();

A nicer version would be: 更好的版本是:

gv.Columns[4].FooterText = ds.Tables[0].AsEnumerable()
.Select(x => x.Field<long?>("Tot1") ?? 0).Sum().ToString();

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

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