Alex Rivera | Logout

Remove all columns with no data from DataTable

Asked 2009-11-19T22:05:22.320
18

If all the items for a particular column are empty, I want to remove that column from the DataTable. What's the most elegant way to do this operation on all columns in the DataTable?

Edit
Report

1 Answer

0
Function RemoveEmptyColumns(Datatable As DataTable) As Boolean
    Dim mynetable As DataTable = Datatable.Copy
    Dim counter As Integer = mynetable.Rows.Count
    Dim col As DataColumn
    For Each col In mynetable.Columns
        Dim dr() As DataRow = mynetable.Select(col.ColumnName + " is   Null ")
        If dr.Length = counter Then
            Datatable.Columns.Remove(col.ColumnName)
            Datatable.AcceptChanges()
        End If
    return true
end function
answered 2012-03-28T21:41:52.903

Your Answer