Can't get DataGridView cell BackColor property - vb.net

I'm trying to retrieve the current color of a DataGridView cell while iterating through the Grid. I am explicitly setting the BackColor for both RowsDefaultCellStyle and AlternatingRowsDefaultCellStyle in the Form Load event.
I'm trying to get the cell BackColor per this question, however, at run time, while iterating through cells in a row, in the Immediate Window this: ?dgvemployees.Rows(rowIndex).Cells(i + 1).Style.BackColor.ToString returns "Color [Empty]" every time - even if I change the indexes to get another cell that I know has the default color set.
Am I missing something or not doing something right?

This page on the Cell Style says:
The DataGridView control displays its cells using the styles indicated
by the cell InheritedStyle property, which inherits styles from other
properties of type DataGridViewCellStyle. The styles specified through
the Style property override the styles specified through all other
cell-style properties, but do not necessarily indicate all the styles
that contribute to the cell's appearance.
Basically, you are trying to access the specific cell back color when it hasn't been set. Even though you have set the back color of the rows, it can be overridden at the cell level.
Thankfully, Microsoft has given us a nice way to find the inherited style of the cell, which will get you the grid-level setting for these cells (unless something further down the chain has overridden that).
?dgvemployees.Rows(1).Cells(2).InheritedStyle.BackColor.ToString()
If I had something else that could override this value and cause problems, then I think I'd be left doing a modulus on the row count to see if it was an alternating row or not.

Related

Difference between Properties and Methods

Im a little confused on the difference between a property and a method. When I think of this, I use the cells default values for my logic and how they change
Default value of a cell is:
Color = blank
Width = 8.43
--If we Set(change) the color to red, then this is referencing a property
--If we auto fit the cells width to say 10(Change the cells width), then this is a method
Either way, the values are being "Changed" from the default settings to a new one. How is it then, that a property and method are not the same??
I understand this logic is wrong. I guess im just trying to think of a better to way to think about it..
Thanks,

VB .NET - Autosize datagridview row height

Problem Definition
So I am trying to do something that should be very basic; however, I can't seem to make it actually work. I am simply trying to make my datagridview autoresize every row height to the text entered. Am I missing something?
What I have tried
I have read through examples on SO and other sites and they all recommend a similar idea. So to make it simple here is exactly what I have done so far:
I created a new datagridview.
I clicked on columns > Add and use the default name and type (textbox)
I kept the Autosizemode of that column at None and DefaultCellStyle WrapMode to True.
I changed the datagridview's AutoSizeRowsMode = AllCells
From there I build my project and type some data in, but the column simply grows the column width and not the row height:
Am I missing a step somewhere? I purposely put every step I did because I feel like I am just missing something very simple...
go to the data grid view -->properties --> default cell style-change the wrap setting to true then use the autosizerow property as any mode like allcells or display cellslike

SSRS Change cell color depending on SUM value

I am loading a dataset into my report and filling my last column with this expression: =Sum(Fields!ID.Value) and it loads fine, I am trying to put a condition in so when the number is less than 15 for example, the cell color will change to red. I have tried so many different syntax but nothing works... it must be pretty simple...?
The errors I am getting are: The value expression for textbox has scope parameter that is invalid for aggregate
Any help would be good :)
To set the background color Click on the cell and in properties window on your right hand select the BackgroundColor property and then set expression to that property.
Or right click on cell and select TextboxProperties -> Fill and at the start there is option to set the expression for fill color.
You are using the wrong expression the expression should be ,
= IIF(Sum(Fields!ID.Value) < 15,"Red","Transparent")
You can change the Transparent to whatever color you want.Take a look here on how to use expressions.
This shows how you could add a colour range if necessary by using the Color property to set the font colour.
=iif(Sum(Fields!ID.Value) < 15,"Red",iif(Sum(Fields!ID.Value)>50,"Blue","Black"))
To Change the background colour instead you would use the Background colour property.
=iif(Sum(Fields!ID.Value) < 15,"Red", "No Color")
Note that SSRS 2008 "Transaparent" is replaced by "No Color". Whilst transaparent works it gives rise to this warning message.
[rsInvalidColor] The value of the BackgroundColor property for the textbox ‘textbox22’ is “Transparent”, which is not a valid BackgroundColor.
As an alternative to these use "#FFFFFF" instead of Transaparent or No Color

DataGridViewRow Color Changes On DataView RowFilter

Goal
To differentiate different rows from each other based on a custom class object's properties. For example, if the object's available state is not available the row backcolor should be displayed in yellow. Similarly, if the object's deleted state is set to true, the row backcolor should be red.
Current Situation
I have a DataGridView that has it's DataSource set to a DataView. The DataView's Table is set to a DataTable filled with data by the custom class.
I created a method that sets the back colors correctly (tested and it works fine). However, this method is executed on the DataGridView1.RowsAdded event. For some reason, even if I have 20 rows that are added, it only goes through the event twice for the row index 0 and row index 1.
Problem
I need a DataGridView event that will fire my method every time a row is added or every time the RowFilter on my DataView changes. How can I go about doing this?
The best way to deal with custom row/cell style is to subscribe to the following events:
RowPrePaint
RowPostPaint
They are only raised when the row is displayed.

how to change the background color of an item in a listbox when its string value is equal to something?

i have a listbox that contains the words "week1", "week2", ..... all the way up to "week52" and when i select a week from the listbox it will retrieve a value from a mysql database that will represent a progress bar value. my progress bar has a range of 0-120 and i would like to have all the weeks that have values higher than 100 to be highlighted or marked somehow, in the listbox. so my question is, "is there a way to set the background color of certain weeks in the listbox to orange based on the value that they represent on the database?
for example for "week1", the value is 114, so when the listbox loads, i want the background color of the item "week1" in the list to be orange (indicating that it's current value is higher than 100)? i know that this requires me to implement a user defined drawing function for the listbox items but i dont know where i would even start. i would like this to be somewhat automatic so that it checks the values and changes the background colors of any value higher than 100, instead of me specifying a name of the item.
Thanks in advance!
I don't believe you can do this with a Listbox (at least not without creating your own implementation/subclassing/overriding/whatever of a Listbox).
Pretty sure you could do it with a ListView (in Detail mode), though, if that helps.