1
  For Each dr In ds.Tables(0).Rows
                        If String.IsNullOrEmpty(dr("BIL")) Then
                            dr.Delete() //how to delete this row?
                        End If
                    Next

首先,将循环所有数据然后检查BIL列中的哪一行为空,如果BIL列中的行为空,则从数据集中删除该行,如何删除这个空数据行?

4

4 回答 4

2

Do you want to delete it in your database or do you want to remove it from the DataTable? Btw, use dr.IsNull("BIL") instead. Your code compiles only because you've set OPTION STRICT off because dr("BIL") returns object instead of string.

Dataset are getting data from EXCEL,so,dont have any identity column.BTW i just want remove from datatable, not database

Then you have to use DataRowCollection.Remove instead of DataRow.Delete. With Delete wthe row will change it's RowState to Deleted. If you then use a DataAdapter to update the DataSet/DataTable or DataRow it will be deleted in the database.

But you can also use Linq-To-DataSet to filter the table and use DataRow.Field extension method which is strongly typed and supports nullable types:

Dim notNullBilRows = From row In ds.Tables(0)
                     Where Not String.IsNullOrEmpty(row.Field(Of String)("BIL")) 

Now you can use CopyToDataTable to create a new DataTable with only rows where BIL is not null, which seems to be the actual requirement.

Dim tblNotNullBilRows  = notNullBilRows.CopyToDataTable()

Here's the non-Linq approach with Remove, you have to create an intermediate collection since you cannot remove elements from a collection during enumeration:

Dim removeList = New List(Of DataRow)
For Each dr As DataRow In ds.Tables(0).Rows
    If String.IsNullOrEmpty(dr.Field(Of String)("BIL")) Then
        removeList.Add(dr)
    End If
Next
For Each dr As DataRow In removeList
    ds.Tables(0).Rows.Remove(dr)
Next
于 2013-04-26T16:06:17.447 回答
1

尝试这个:

For i As Integer = dt.Rows.Count - 1 To 0 Step -1
  If String.IsNullOrEmpty(dt.Rows(i)("BIL")) Then
    dt.Rows.RemoveAt(i)
  End If
Next
于 2013-04-26T16:22:55.770 回答
0

您需要将要删除的行的索引放入一个数组中,然后遍历该数组,使用索引从数据表中删除每一行。您不需要“身份”列来执行此操作,行将自动分配索引。

于 2013-04-26T15:57:48.507 回答
0

假设您在表tbl中有 2 列:ColumnAColumnB

Dim dv as new DataView
dv = new DataView(tbl)
dv.RowFilter = "ColumnA <> '' AND ColumnB <> ''"
tbl = dv.ToTable()

tbl不应再有空行。希望这可以帮助。

于 2019-04-30T00:04:21.040 回答