c# - How to access a cell in a DataRowView based on the columns DataPropertyName? -


i have windows forms application dataset. i've used data | add new datasource add products table of northwind database datasources , created datagridview showing contents of products table. dragged products table data sources window form, columns created automatically.

now, want rows containing product discontinued column true painted in different color. i've created cellpainting event handler it, i'm having trouble locating value discontinued column.

because datagridview created automatically, columns in have names datagridviewtextboxcolumn1, has datapropertyname of "productid".

my question is: how can find value discontinued based on datapropertyname? or required use name of column itself? (in case better give meaningful name)

my code is:

private void productsdatagridview_cellpainting(object sender,     datagridviewcellpaintingeventargs e) {     if (e.rowindex >= 0)     {         var row = productsdatagridview.rows[e.rowindex];         if ((bool) (row.cells[ nameorindexofcolumn ].value))         {             e.cellstyle.forecolor = color.darkgray;         }     } } 

how can access value of column datapropertyname "discontinued"?


solution

based on neil barnwell's answer, seems way.

private void productsdatagridview_cellpainting(object sender, datagridviewcellpaintingeventargs e) {     if (e.rowindex >= 0)     {         var productview = (datarowview) productsdatagridview.rows[e.rowindex].databounditem;         var product = productview.row northwinddataset.productsrow;         if (product != null && product.discontinued)         {             e.cellstyle.forecolor = color.darkgray;         }     } } 

the big advantage of discontinued value doesn't have actual column on datagridview.

don't value column in grid, value actual datarow populates gridrow. way can avoid magic strings etc.

there's little bit of casting done because [type]datarow hidden inside dataview that's attached grid, more elegant approach (and far less brittle in case of future changes) if integrate nicely code relying on magic strings.

here's old blog post of mine describes in detail how it:

http://koder.wordpress.com/2010/04/09/getting-data-from-a-winforms-datagridview/

update
you've mentioned you're using northwind , you've "simply dragged products table form", i'm guessing isn't mission-critical piece of software, benefit of other people reading, wanted suggest wouldn't typical approach more in real application.

typically these days we'd consider having domain model, perhaps using orm obtain our domain objects data store (of course datasets aren't real orm), possibly using things mvvm build data structures optimised binding ui elements domain objects.

using approach, because have actual data hand in viewmodel, can calculate such rules colours etc real data, , ui displays results of applying business rules.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -