Gatt (22nd July 2008)

Help!!
Writing a small app that has a datagrid view
Some of the columns are dataGridViewComboBoxColumns with 3 options
Not Started,Completed and UnFinished
What I want to do is -
If "Unfinished" is selected I want the cell to turn RED
If "Completed" is selected I want the cell to turn GREEN
I have read that this needs to be done using the CellPainting event and have the following event in my code:
Unfortunately its not workingCode:private void dataGridView1_CellPainting(object sender, DataGridCellPaintingEventArgs e) { try { if (e.ColumnIndex == datagridView1.Columns["Task1"].Index) { if (e.Value.Equals("Unfinished")) { e.CellStyle.BackColor = Color.Red; } } } catch { } }
Anyone got any ideas?
e.CellStyle.BackColor = Color.Red
Do you not have to initialize a solid brush for your colour (a paint event)
backColorBrush = new SolidBrush(e.CellStyle.BackColor);
You may have done this already but you post very little of your code.
I have never had need to do this before and turned out to be trickier than I first imagined, as getting at events when a combo box is part of a gridView proved to the sticking pojnt for me.
Hard to understand the context of what you are doing, but here is an example of something that achives a similar result using the CellValidated event. ie if you change a value, it will check it and change colour. Is there a reason why you are using a Paint event?
PS above would be better as a select case so default would be Color.White (or what ever default cell colour is)Code:private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e) { // MessageBox.Show(dataGridView1.CurrentCell.EditedFormattedValue.ToString()); if (dataGridView1.CurrentCell.EditedFormattedValue.ToString() == "1") { dataGridView1.CurrentCell.Style.BackColor = Color.Green; } if (dataGridView1.CurrentCell.EditedFormattedValue.ToString() == "2") { dataGridView1.CurrentCell.Style.BackColor = Color.Red; } }
Last edited by monkeyx; 22nd July 2008 at 12:46 PM.
Gatt (22nd July 2008)

@monkeyx - cheers thats helped a little
Though its not changed the acutal cell yet - its changed the colour of the dropdown menu!
Only reason for the Paint event was cos that was all i could get from Google...
Edit
Sorted! Changed the "DisplayStyleForCurrentCellOnly" property of the column to true and its working
Just need to stop it changing teh drop menu now!!
Last edited by Gatt; 22nd July 2008 at 12:53 PM.

Just to clarify the context of this
I'm working on an app for the ICT teachers to monitor a class' (or is it class's) progress through the OCR modules and tasks..
The app saves the data in an XML file that the teacher can load to update progress
The three columns of interest at present are Task 1, Task 2, and Task 3.
These are a combobox with 3 options - Not Started, Unfinished and Completed which allows the teacher to select the current progress of each task per pupil
I have now, thaks to MonkeyX got the colouring working so that each "state" is colour coded (e.g. Completed is Green)
All working pretty much as it should with 2 new minor blips.
Blip 1
The background of the ComboBox list changes to the colour of the cell - no big deal really
Blip 2
This ones a bit more of an issue - when I load an XML into the DataGrid - i have to click all the above cells to get the colouring to work as it only validates when you leave the cell..
So..
Anyone know how i get cells to be validated when the XML file is loaded into the DataGridd?
I'll keep looking and will try get a demo online at somepoint.
Hmm a quick looks on google showed me this
Format single Row in datagridview using a value in same row - C#
Does that help?
Gatt (22nd July 2008)

@monkeyx - this might very well!
I'll give it a bash and let you know!
There are currently 1 users browsing this thread. (0 members and 1 guests)