Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I'm hoping to find a way to exclude blank excel cells when I import my spreadsheet using the oledbadapter. I know it must be something like "If datagridview1.columns() = dbnull then don't include them. Here is my code below.


Maybe something along the lines of this will work. It still isn't working for me though.

VB
Dim Empty As Boolean = True

For i As Integer = 0 To dataGridView1.Rows.Count - 1
    Empty = True
    For j As Integer = 0 To dataGridView1.Columns.Count - 1
        If dataGridView1.Rows(i).Cells(j).Value IsNot Nothing AndAlso dataGridView1.Rows(i).Cells(j).Value.ToString() <> "" Then
            Empty = False
            Exit For
        End If
    Next
    If Empty Then
        dataGridView1.Rows.RemoveAt(i)
    End If
Next
Posted
Updated 7-Aug-13 4:26am
v2

1 solution

There is no need to Fill the Grid and loop that to find empty row.

If you Fill DataGridView using a DataTable then solve the Empty row issue in DataTable onwards...

Try like below...

VB
dataTable = dataTable.Rows.Cast(Of DataRow)().Where(Function(row) Not row.ItemArray.All(Function(field) TypeOf field Is System.DBNull OrElse String.Compare(TryCast(field, String).Trim(), String.Empty) = 0)).CopyToDataTable()

GridView.DataSource = dataTable


It will remove all the empty rows and assign the value to gridview..
 
Share this answer
 
Comments
CAS1224 7-Aug-13 12:10pm    
Thanks for your answer. I'm not exactly sure how to apply this though. Is there another way?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900