1
votes

Having set up a datagridview, bound to a bindingsource, I have to use the cellformatting to paint the cells various colours.

Since you cannot access a field or record via bindingsource on a cellformatting event, the only way I could access the data, is through a column such as the primary ID for example. (this is only for cellformatting, i have no problem accessing the ID through the bindignsource.current object).

So, once I have finished designing and testing my datagridview, I then switch various columns (ID) for this example, to visible=false to make the column invisible.

I now cannot access the column data through cellformatting. I have read on a number of pages listed by google that since datagridview doesnt render the columns, the values are unobtainable aside from a couple of alternatives such as : .TAG and datakeynames

I cannot find datakeynames under datagridview for winforms for some reason. .tag doesnt seem to do anything as far as I can tell.

Since trying to use this from the cell formatting, I am unable to use

Select Case .Columns(e.ColumnIndex).DataPropertyName.ToLower

or

If Datagridview1.columns(e.columnindex).datapropertyname.tolower='id' Then

as the column is invisible...

Shame I cannot access the bindingsource through cellformatting, but there must be a way and there must be others who have had this issue.

1
You can access invisible columns by index. It is not uncommon to hide some value there and evaluate it to color a row. Many events wont fire for a cell/column which is not seen (off screen or invisible) but that doesnt preclude you from evaluating that column's data when it fires for other cells/columns. Its not clear why you are after things other than the value - Ňɏssa Pøngjǣrdenlarp
What are you doing to the data that you couldn't do without a formatting event? Really, the data shouldn't be manipulated based on formatting, i.e. store some logic corresponding to the formatting in the data source to take all the logic off the UI. Follow separation of concerns even (especially) on the UI - djv
DJV - Perhaps you could help me out with that, as I hate using the cell formatting event, it constantly fires but I cannot see any way around <not> using it. The columns are manually created through code, the cellformatting, reads certain columns and then adjusts the backcolor of certain cells or puts a graphic in those cells (depending on the values). Some cells are read for a certain value, then I am looking at a different cell to check the value of that - sometime more than one combination of values from multiple cells, set the value of the cell that I is currently being accessed - Carl Harrison

1 Answers

0
votes

Use DataBoundItem:

Dim drw As DataRow = DirectCast(DataGridView1.Rows(i).DataBoundItem, DataRowView).Row

(Assuming you've bound to a DataTable)