Monday, June 22, 2015

Programming: Remove Empty Rows in a DataTable when importing from Excel

Background

Simple bug a tester found on a bulk importing application that was using OLEDB to scrape an Excel Spreadsheet (notorious Sheet1$). Googling showed some answers on Stack Overflow however they all ended up using a separate list which didn't fit our problem. We need to set the PrimaryKey field of the the DataTable later in our code.

Problem Space 

How does one delete the empty rows in the DataTable Rows collection itself?

Solution Space

Use Remove() in conjunction with a LINQ query to get all the empties as object references.


   
    //Any row that is has an empty string in the crucial column "Primary Key" needs to be removed.
    var empties = table
        .Rows
        .Cast<DataRow>()
        .Where(
            row => string.IsNullOrEmpty(row[primaryKey].ToString())
        ).ToList();

        foreach (var empty in empties)
        {
            //Empties begone!
            table.Rows.Remove(row: empty);
        }

        var rows = table.Rows.Cast<DataRow>().ToList();
        foreach (var row in rows)
        {
            //You're code goes here!
        }

No comments:

Post a Comment