2

I need to remove the row from datatable even if any column in the datatable has empty values.

For Example :

My datatable has two columns

 Value     Input
  Dep     Phase 1
  Sec     
  Div     Phase 2

Result

 Value     Input
  Dep     Phase 1
  Div     Phase 2

I want to remove the second row as Input column has empty values. I'm stuck with the below code.

dtTable = dtTable.Rows.Cast<DataRow>()
          .Where(row => !row.ItemArray.All(field => field is DBNull || string.IsNullOrWhiteSpace(field as string))).
          .CopyToDataTable();

Can anyone suggest me how this can be achieved ?

3
  • what is the issue you facing ? Commented Mar 6, 2018 at 12:31
  • 4
    you need Any not All Commented Mar 6, 2018 at 12:31
  • @RahulAgarwal - I'm not able to delete a row even if any of its column has empty values. Commented Mar 6, 2018 at 12:33

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

what if column is not a string?
then we would need to do some work to check for exact type or we can convert it to string via .ToString()
@Alexey, null should be converted to empty string and then after trim() function check length of that string either zero or not?
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();
      }
    }

Comments

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

1 Comment

Code-only answers are not particularly helpful. Please add some descriptions of how this code solves the problem.
0
 for (int i = dt.Rows.Count - 1; i >= 0; i--)
                {
                    if (dt.Rows[i][1] == DBNull.Value)
                    {
                        dt.Rows[i].Delete();
                    }
                }
                dt.AcceptChanges();

it remove empty rows in th data table

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.