Best way to check if a Data Table has a null value in it

摘要:Best way to check if a Data Table has a null value in it

foreach(DataRow row in table.Rows)
{
    object value = row["ColumnName"];
    if (value == DBNull.Value)
        // do something
    else
        // do something else
}

If you want to check if a null value exists in the table you can use this method:

public static bool HasNull(this DataTable table)
{
    foreach (DataColumn column in table.Columns)
    {
        if (table.Rows.OfType().Any(r => r.IsNull(column)))
            return true;
    }

    return false;
}