Coding Thread, C# Help - Datagrid cell formatting in Coding and Web Development; Help!!
Writing a small app that has a datagrid view
Some of the columns are dataGridViewComboBoxColumns with 3 options
Not ...
-
21st July 2008, 09:29 PM #1 C# Help - Datagrid cell formatting
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:
Code:
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 { }
} Unfortunately its not working 
Anyone got any ideas?
e.CellStyle.BackColor = Color.Red
-
-
IDG Tech News
-
21st July 2008, 09:54 PM #2 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.
-
-
22nd July 2008, 12:39 PM #3 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?
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;
}
} PS above would be better as a select case so default would be Color.White (or what ever default cell colour is)
Last edited by monkeyx; 22nd July 2008 at 12:46 PM.
-
-
22nd July 2008, 12:46 PM #4 @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.
-
-
22nd July 2008, 01:17 PM #5 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.
-
-
22nd July 2008, 03:05 PM #6
-
-
22nd July 2008, 08:33 PM #7 @monkeyx - this might very well!
I'll give it a bash and let you know!
-
SHARE: 
Similar Threads
-
By laserblazer in forum General Chat
Replies: 10
Last Post: 27th June 2008, 09:12 AM
-
By Jobos in forum How do you do....it?
Replies: 3
Last Post: 3rd June 2008, 01:58 PM
-
By randle in forum Windows
Replies: 3
Last Post: 25th October 2007, 01:20 PM
-
By wesleyw in forum How do you do....it?
Replies: 3
Last Post: 20th December 2006, 10:01 PM
-
By chrbb in forum Windows
Replies: 6
Last Post: 21st February 2006, 11:36 AM
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules