![]() | Register | FAQ | Members | Social Groups | User Map | Calendar | Search | Today's Posts | Mark Forums Read |
| | | LinkBack | Thread Tools | Search Thread | Language |
| Sponsored Links |
| | #1 |
![]() | 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 { }
}
Anyone got any ideas? e.CellStyle.BackColor = Color.Red |
| |
| | #2 |
![]() Join Date: Jun 2005
Posts: 255
Thanks: 1
Thanked 9 Times in 7 Posts
Rep Power: 8 | 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. |
| |
| | #3 |
![]() Join Date: Nov 2006
Posts: 181
Thanks: 3
Thanked 13 Times in 11 Posts
Rep Power: 7 | 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;
}
}
Last edited by monkeyx; 22-07-2008 at 12:46 PM.. |
| |
| The Following User Says Thank You to monkeyx For This Useful Post: | Gatt (22-07-2008) |
| | #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; 22-07-2008 at 12:53 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. |
| |
| | #6 |
![]() Join Date: Nov 2006
Posts: 181
Thanks: 3
Thanked 13 Times in 11 Posts
Rep Power: 7 | Hmm a quick looks on google showed me this Format single Row in datagridview using a value in same row - C# Does that help? |
| |
| The Following User Says Thank You to monkeyx For This Useful Post: | Gatt (22-07-2008) |
| | #7 |
![]() | @monkeyx - this might very well! I'll give it a bash and let you know! |
| |
| |
| | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Cell Phone signal. | laserblazer | General Chat | 10 | 27-06-2008 09:12 AM |
| Conditional formatting help needed | Jobos | How do you do....it? | 3 | 03-06-2008 01:58 PM |
| Help with conditional formatting | randle | Windows | 3 | 25-10-2007 01:20 PM |
| Excel Cell colour changes | wesleyw | How do you do....it? | 3 | 20-12-2006 11:01 PM |
| formatting w98 | chrbb | Windows | 6 | 21-02-2006 12:36 PM |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search Thread |
|
|






