score:0
Try this:
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
if (dt.Rows[i]["Column2"].ToString() == "")
{
dt.Rows[i].Delete();
dt.AcceptChanges();
}
}
score:1
According to the requirements you are mentioning you would need to use Any
which will check that if at least column has null
value or empty string it would filter out that row. So, do like:
row => !row.ItemArray.Any(field => field is DBNull ||
string.IsNullOrWhiteSpace(field as string))
EDIT:
If your all columns are of type string
then the above code would work fine, otherwise you will need to convert it to string explicitly by calling ToString()
:
row => !row.ItemArray.Any(field => field is DBNull ||
string.IsNullOrWhiteSpace(field.ToString()))
This will now only return those rows for which every column has value in it.
score:1
If for loop can be used. We can check each row and column for nulls
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
if (dt.Rows[i]["col1"] == DBNull.Value || dt.Rows[i]["col2"] == DBNull.Value)
{
dt.Rows[i].Delete();
}
}
Source: stackoverflow.com
Related Query
- Remove datatable row if any column value is empty c#
- C# - Remove rows with the same column value from a DataTable
- LINQ to remove duplicate rows from a datatable based on the value of a specific row
- Querying for a specific value at the intersection of a Row and a Column in a DataTable
- Remove rows with same column value from DataTable and add corresponding values
- How to Find a DataTable Row by Column Value Using LINQ in VB.NET
- Find duplicate row in a datatable by id - add the duplicate column values as concatented string to a new column then remove dulplicate column
- Partition By Logic in Code to calculate value of a DataTable Column
- datatable in vb.net keep rightmost column with data in and remove empty columns
- Searching for a value in a DataTable column returns empty when the value exists
- Linq : select value in a datatable column
- Get excel cell value with Row and Column Position through open xml sdk linq query
- Using LINQ to get DataGridView row index where first column has specific value
- LINQ to separate column value of a row to different rows in .net
- Using LINQ to remove any value that is a duplicate
- LINQ left join with only the row having maximum value of a column
- linq remove items from query where any list value is present
- The given value of type String from the data source cannot be converted to type int of the specified target column
- skip entire row if particular cell value contains zero in datatable C#
- DataTable Column value grouping and sorting
- Get Max from column datatable or Default Value using Linq
- how to get the row value of each column from linq
- Is there any other way to convert row fields into column other than pivot
- How to solve cast not valid when Get a Specific Column Value from a DataTable in this example in C#?
- Linq to dataset select row based on max value of column
- How to select max and min value of any column of datagridview
- How to select rows from a DataTable where a Column value is within a List?
- How can I set the value of a column for every row in a table using LINQ?
- How to remove any value from Dictionary<string,List<string>> using LINQ
- Find All Rows in DataTable Where Column Value is NOT Unique Using Linq Query
More Query from same tag
- How to convert this TSQL into Linq Method approach
- Split an single-use large IEnumerable<T> in half using a condition
- How to use LINQ to XML to retrieve nested arrays?
- Getting Href property with anglesharp linq query
- Get the Value of XElements that dont has Child
- Performing operations on a collection of string arrays
- Get a count of elements that appear once in a list with LINQ
- Query JSON Nested Object using LINQ
- Removing items from a list where the Id is in another list
- LINQ Group and SelectMany
- How to convert this sql part to linq
- Linq filtering in a property's getter
- Recursive linq query
- filter list where object property is equal to a string
- LINQ to identify a row that is the first in consecutive occurrences that meet criteria
- .First does not throw exception on empty collection
- LINQ C# - Writing a query to display data in a pivot style
- Filter from table for column having comma and pipe separated values
- How to use OrderBy Linq when object is null and property is integer
- How to add new node just after parent node in XML
- Double Filtering in LINQ
- Linq order based on some other column condition
- LINQ Count problems while trying to access to subList from a List of Objects (VB.NET)
- How to access an already defined property of an anonymous linq object?
- Creating a dynamic EF filter that builds a LINQ Where equals/contains statement for any string entity property
- How to get matched records from Dictionary Model and List Model in ASP.NET Core MVC
- max and group by question with LINQ
- Break from function composition on condition using LINQ
- Relate two lists with LINQ extensions
- LINQ - mandatory field and dont care if null