C# DataRow Empty-check

后端 未结 11 1130
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 05:13

I got this:

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

Then I add data to the row (or not). Lots of code, real

11条回答
  •  甜味超标
    2020-12-15 05:56

    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;
     }
    }
    

    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
    {
    }
    

提交回复
热议问题