Hi
I consider myself fairly new to VB.NET, so maybe you could help me here.
I have a bindingSource which is bound to a dataset. I have a filter on the bindingsource. My problem is that when I add a new row, with BindingSource.AddNew, even if it does not match the filter criteria, it still isn't filtered out.
Could it be that the bindingsource filter never filters out detached rows? If so, what can I do in this situation?

Example

Me.TblStempelkarteBindingSource.Filter = "Month = " & getCurrentDate.Month & _
                                                " AND  Day = " & getCurrentDate.Day & _
                                                " AND  Year = " & getCurrentDate.Year

I add a new row to the BindingSource and then do BindingSource.Count. The count always includes the added row, even if the fields do not match Month/Day/Year of my filter.
Thank You!

Dani AI

Generated

Short answer: the row you see after calling BindingSource.AddNew is a provisional (pending) item that the BindingSource tracks for transactions, so it can show up in Count even when it doesn't match the Filter. That is why simply re-applying the filter did not remove it — the BindingSource / currency manager is holding a pending add separate from the finalized DataView rows. (learn.microsoft.com)

Why this happens: when a BindingSource is bound to a DataTable it works through a DataView; AddNew/AddingNew create a DataRowView in an edit/pending state and the change is not committed into the table until you call EndEdit (or the underlying collection commits). The DataView only evaluates RowFilter/Filter against committed/current row values, so the transient pending-add behavior means the temporary item can bypass the normal filter behavior until it is committed or cancelled. (learn.microsoft.com)

What to do (practical fixes):

  • If you want the new row to be evaluated by the filter immediately, create and add a DataRow to the DataTable (NewRow → set fields → Rows.Add). That puts the row into the table and the view will include or exclude it according to your filter. (learn.microsoft.com)

  • If you need the UI AddNew workflow (user types then save), commit the pending item after editing so the filter is re-applied. Example pattern (VB.NET):

    Dim drv = CType(TblStempelkarteBindingSource.AddNew(), DataRowView)
    drv("Month") = getCurrentDate.Month
    drv("Day") = getCurrentDate.Day
    drv("Year") = getCurrentDate.Year
    drv.EndEdit()                ' commits into the table -> DataView will re-evaluate the filter
    TblStempelkarteBindingSource.EndEdit()
  • To drop the provisional row instead, call CancelEdit / CancelNew / RemoveCurrent as appropriate; BindingSource supports transactional add semantics (ICancelAddNew). (learn.microsoft.com)

Quick debug tips: cast the current item to DataRowView and inspect drv.Row.RowState while stepping through AddNew; that shows whether the row is still pending vs. Added/Unchanged and helps verify whether EndEdit fixed the filter behavior.

Recommended Answers

All 4 Replies

you need to filter your dataview again

I removed the filter and then applied it again. Doesn't seem to work. Any other suggestions?

Can you provide a bit more code? Like the whole function?

I didn't find a solution to this problem, but I was able to work around it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.