有没有办法在datagridview中使用oledbadapter在导入时排除空白excel单元格? [英] Is there a way to exclude blank excel cells on import using oledbadapter in a datagridview?

查看:75
本文介绍了有没有办法在datagridview中使用oledbadapter在导入时排除空白excel单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在使用oledbadapter导入电子表格时找到一种排除空白excel单元格的方法。我知道它必须是如果datagridview1.columns()= dbnull然后不包括它们。这是我的代码。





也许这样的事情会起作用。但它仍然不适合我。



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.

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

推荐答案

无需填充网格并循环查找空行。



如果你使用DataTable填充DataGridView然后解决DataTable中的空行问题...



尝试如下...



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...

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





它将删除所有空行并将值赋给gridview ..



It will remove all the empty rows and assign the value to gridview..


这篇关于有没有办法在datagridview中使用oledbadapter在导入时排除空白excel单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆