Ask Your Question
2

How can I remove rows with null values in a dataTable?

asked 2021-12-22 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-03-24 14:00:00 +0000

bukephalos gravatar image

You can remove rows with null values in a dataTable by using the following code:

dataTable.Rows.Cast<DataRow>().Where(row => row.ItemArray.All(field => field is DBNull)).ToList().ForEach(row => dataTable.Rows.Remove(row));

This code will loop through all the rows in the dataTable and check if all the fields in the row are null using the LINQ method "All". If all the fields are null, the row will be removed from the dataTable using the "Remove" method.

Note: Make sure to replace "dataTable" with the name of your actual dataTable variable.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-22 11:00:00 +0000

Seen: 16 times

Last updated: Mar 24 '23