C#的DataRow空的检查 [英] C# DataRow Empty-check

查看:679
本文介绍了C#的DataRow空的检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个:

 DataTable dtEntity = CreateDataTable();
 drEntity = dtEntity.NewRow();



然后我将数据添加到行(或没有)。代码
地块,真的不知道,如果有该行内部的任何物件。
依赖于输入(我从一些文件导入)。
我想要做的是这样的:

Then I add data to the row (or not). Lots of code, really don't know if there's anything inside the row. Depends on the input (i am importing from some files). I'd like to do something like:

 if (drEntity`s EVERY CELL IS NOT EMPTY)
 {
   dtEntity.Rows.Add(drEntity);
 }
 else
 {
   //don't add, will create a new one (drEntity = dtEntity.NewRow();)
 }

有一些不错的方法来检查,如果DataRow中的每一个细胞是空的?
或我的foreach,和一个检查他们有一个

Is there some nice way to check if the DataRow's every cell is empty? Or I should foreach, and check them one by one?

推荐答案

线沿线的一个简单的方法:<? / p>

A simple method along the lines of:

bool AreAllColumnsEmpty(DataRow dr)
{
 if (dr == null)
 {
  return true;
 }
 else
 {
  foreach(var value in dr.ItemArray)
  {
    if (value != null)
    {
      return false;
    }
  }
  return true;
 }
}



应该给你什么你之后,并让好(因为就没有什么是我所知,在Framework),你可以把它包起来作为一个扩展方法,然后将产生的代码如下:

Should give you what you're after, and to make it "nice" (as there's nothing as far as I'm aware, in the Framework), you could wrap it up as an extension method, and then your resultant code would be:

if (datarow.AreAllColumnsEmpty())
{
}
else
{
}

这篇关于C#的DataRow空的检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆