How To Add New Rows to a Filtered DataView

Suppose you have a “view” into a set of records. One column of the data contains a country ID, (such as “US”):

 DataView myDataView = new DataView(myTable); myDataView.RowFilter = "country = 'us'"; myGrid.DataSource = myDataView; 

If you filter the records so that only the “US” records show up in your view, and then add a new record, the new record won’t show up?because it’s filtered out.

DataRowView drv = myDataView.AddNew(); 

To solve this problem, you might spend considerable effort debugging. Or maybe your immediate reaction would be to victimize the datagrid (which could possibly be the third-party component). But the reality is that the problem is with the code! Take a look:

myDataView.RowFilter = "country = 'us'", 

Due to this filter, dataview would have only those rows which satisfy the condition that all other rows are not part of the view. So when a new row is added, the value of drRow[“country”] is DBNull due to the fact that the new row is not part of filtered dataview.

Here’s the solution to this problem:

 // Change values in the DataRow. drRow = drv.Row[0]; drRow ["country"]='us' drRow.EndEdit(); 

Now the new row will be visible in the grid.

Note: If your filter expression has multiple columns, all the columns must be set explicitly in the new row so that the filter condition is satisfied.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet