vb.net DataView 将自身更新为新的 rowfilter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16960096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:52:16  来源:igfitidea点击:

DataView Update itself to the new rowfilter

vb.netc#-4.0datatabledataview

提问by Tajamul Iqbal

I am trying to retain some rows in dataview for some operations to perform after I get the filtered rows, but the issue is , every time the code runs, the dataview is updated to the filter last applied. Here is the code snippest.

我试图在数据视图中保留一些行,以便在获得过滤后的行后执行某些操作,但问题是,每次代码运行时,数据视图都会更新为上次应用的过滤器。这是代码片段。

If dsEmp.Tables.Count > 0 Then
    dvEmp = dsEmp.Tables(0).DefaultView
    dvEmp.RowFilter = "IDENTIFICATION_CODE = '" & IDENTIFICATION_CODE & "' and EMPLOYEE_DESC =  'Employment1'"
    Dim dv1 As DataView = dvEmp
    dvEmp.RowFilter = ""
    If dsEmp.Tables(0).Rows.Count > 1 Then
        dvEmp2 = New DataView
        dvEmp2 = dsEmp.Tables(0).DefaultView
        dvEmp2.RowFilter = "IDENTIFICATION_CODE = '" & IDENTIFICATION_CODE & "' and EMPLOYEE_DESC =  'Employment2'"
        Dim dv2 As DataView = dvEmp2
        dvEmp2.RowFilter = ""
    End If
    If dsEmp.Tables(0).Rows.Count > 2 Then
        dvEmpPrevious = New DataView
        dvEmpPrevious = dsEmp.Tables(0).DefaultView
        dvEmpPrevious.RowFilter = "IDENTIFICATION_CODE = '" & IDENTIFICATION_CODE & "' and EMPLOYEE_DESC =  'Employment3'"
        Dim dv3 As DataView = dvEmpPrevious
        dvEmpPrevious.RowFilter = ""
    End If
End If

but when I check the value of dv1, after second filter, it has the different values in it.

但是当我检查 dv1 的值时,在第二个过滤器之后,它有不同的值。

Could any one give some solution. Thanks

任何人都可以提供一些解决方案。谢谢

采纳答案by Steve

You are just changing the name of the variables but every DataView above is still referencing the same DataView (the DefaultView of the table). So when you reset the RowFilter you apply this change to the DefaultView as well.

您只是更改了变量的名称,但上面的每个 DataView 仍然引用相同的 DataView(表的 DefaultView)。因此,当您重置 RowFilter 时,您也会将此更改应用于 DefaultView。

If you want to have different views with different filters then you need to create a DataView passing the source DataTable

如果你想拥有不同过滤器的不同视图,那么你需要创建一个传递源 DataTable 的 DataView

dvEmp = new DataView(dsEmp.Tables(0))
dvEmp.RowFilter = "IDENTIFICATION_CODE = '" & IDENTIFICATION_CODE & "' and EMPLOYEE_DESC =  'Employment1'"

Now the dvEmp is no more referencing the DefaultView of the DataTable and it should maintain the correct RowFilter applied

现在 dvEmp 不再引用 DataTable 的 DefaultView,它应该保持应用正确的 RowFilter

Of course, you should use the filtered DataView to test the number of records present in the DataView after the filter not the DataTable.
The DataTable will contain always the same number of records

当然,您应该使用过滤后的 DataView 来测试过滤后 DataView 中存在的记录数,而不是 DataTable。
DataTable 将始终包含相同数量的记录

If dvEmp.Count > 1 Then
   .....