Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DataRow Empty-check

Tags:

I got this:

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

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?

like image 230
Ash Avatar asked Feb 24 '10 13:02

Ash


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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 { } 
like image 199
Rob Avatar answered Jan 09 '23 07:01

Rob