locked
Add a new row in DataView with RowFilter RRS feed

  • Question

  • User1556807812 posted

    I have a dataview which has row filters for multiple columns

        Public Function GetDataViewWithFilter(CurrentFilter As String) As DataView
           Dim ActiveDataView = New DataView(DataTable)
           ActiveDataView.RowStateFilter = DataViewRowState.CurrentRows
           ActiveDataView.RowFilter = CurrentFilter 
           Return ActiveDataView 
        End Function
    
        Public Sub AddNew()
            If ActiveDataView IsNot Nothing Then
                Dim rv As DataRowView = ActiveDataView.AddNew           
                rv.EndEdit()       
                'Here comes the problem
                'ActiveDataView.Count() exclude new row from row count because new row does not contain the filter
            End If        
        End Sub
    


    Is there any way, that new row will still exists after DataRowView.EndEdit? Or I can copy The existing RowFilter values into the newly created row.

    Thursday, August 20, 2015 11:15 AM

All replies

  • User-271186128 posted

    Hi joru221,

    Welcome to asp.net forum.

    Is there any way, that new row will still exists after DataRowView.EndEdit? Or I can copy The existing RowFilter values into the newly created row.

    From your description, it seems that you want to insert the filtered values into the DataView. If that is the case, please refer to the following sample:

        Sub Main()
            DemostrateDataView()
    
        End Sub
        Private Sub DemostrateDataView()
            ' Create a DataTable with one column 
            Dim dt As New DataTable("MyTable")
            Dim column As New DataColumn("Col", GetType(Integer))
            dt.Columns.Add(column)
    
            ' Add 5 rows on Added state 
            For i As Integer = 0 To 4
                Dim row As DataRow = dt.NewRow()
                row("Col") = i
                dt.Rows.Add(row)
            Next
    
            ' Create a DataView 
            Dim dv As New DataView(dt)
    
            Console.WriteLine("Print unfiltered DataView")
            PrintDataView(dv)
    
            Dim rowview As DataRowView = dv.AddNew()
            rowview("Col") = 6
            rowview.EndEdit()
    
            Dim dv2 As New DataView()
            dv2 = dv
            Console.Write("Print dv2")
            PrintDataView(dv2)
    
            Console.WriteLine("Print the added DataView")
            PrintDataView(dv)
    
            ' Filter by an expression. Filter all rows where column 'Col' have values greater or equal than 3
            dv.RowFilter = "Col < 3"
    
            Console.WriteLine("Print Filtered DataView by RowFilter. RowFilter = 'Col > 3'")
            PrintDataView(dv)
    
            For Each drv As DataRowView In dv
                Dim newRowView = dv2.AddNew()
                newRowView("Col") = drv("Col")
                newRowView.EndEdit()
            Next
    
            ' Removing RpwFilter.
            dv.RowFilter = [String].Empty
            dv2.RowFilter = String.Empty
            Console.WriteLine("Print the final DataView")
            PrintDataView(dv2)
            Console.ReadKey()
    
        End Sub
    
        Private Sub PrintDataView(dv As DataView)
            ' Printing first DataRowView to demo that the row in the first index of the DataView changes depending on sort and filters
            Console.WriteLine("First DataRowView value is '{0}'", dv(0)("Col"))
    
            ' Printing all DataRowViews 
            For Each drv As DataRowView In dv
                Console.WriteLine(vbTab & " {0}", drv("Col"))
            Next
        End Sub

    The output:

    Best Regards,
    Dillion

    Sunday, August 23, 2015 11:15 PM
  • User1556807812 posted
    it seems that you want to insert the filtered values into the DataView.


    No this is not the case. My issue is to copy the filter values dynamically to the new row, So that after EndEdit it still appears it on the view

    Tuesday, August 25, 2015 7:51 AM
  • User-271186128 posted

    Hi joru221,

    My issue is to copy the filter values dynamically to the new row, So that after EndEdit it still appears it on the view

    Please refer to my previous reply, I create a new DataView (dv2) to save the original data which contains in the DataView (dv). After filtering row using RowFilter, I use for each statement to insert the Filter rows into the dv2. So, now the dv2 contains the original data and the Filter values.

    Best Regards,
    Dillion

    Tuesday, August 25, 2015 11:14 PM
  • User1556807812 posted

    In my case, i do not have the column names and I do not know which columns are filtered and what are the filtered values

    Wednesday, August 26, 2015 6:46 AM