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?
A simple method along the lines of:
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:
You could use this:
IsNotEmpty(cell)
would be your own implementation, checking whether the data is null or empty, based on what type of data is in the cell. If it's a simple string, it could end up looking something like this:Still, it essentially checks each cell for emptiness, and lets you know whether all cells in the row are empty.
DataTable.NewRow
will initialize each field to:the default value for each
DataColumn
(DataColumn.DefaultValue
)except for auto-increment columns (
DataColumn.AutoIncrement == true
), which will be initialized to the next auto-increment value.and expression columns (
DataColumn.Expression.Length > 0
) are also a special case; the default value will depend on the default values of columns on which the expression is calculated.So you should probably be checking something like:
I'll leave the LINQ version as an exercise :)
Maybe a better solution would be to add an extra column that is automatically set to 1 on each row. As soon as there is an element that is not null change it to a 0.
then
I prefer approach of Tommy Carlier, but with a little change.
I suppose this approach looks more simple and bright.
I did it like this: