Welcome, Register for free! or Login below:
EduGeek.net RSS Feeds Register FAQ Members Social Groups User Map Calendar Search Today's Posts Mark Forums Read

Go Back   EduGeek.net Forums > Coding and Web Development > Coding
Reply
 
LinkBack Thread Tools Search Thread Language
Sponsored Links
Old 21-07-2008, 09:29 PM   #1
 
Gatt's Avatar
 
Join Date: Jan 2006
Location: Moorside High, Swinton / Middleton, Rochdale
Posts: 2,135
uk uk scotland
Thanks: 80
Thanked 36 Times in 27 Posts
Rep Power: 19 Gatt is a jewel in the roughGatt is a jewel in the roughGatt is a jewel in the rough
Send a message via MSN to Gatt
Default 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
  Reply With Quote
Old 21-07-2008, 09:54 PM   #2
 
petectid's Avatar
 
Join Date: Jun 2005
Posts: 255
Thanks: 1
Thanked 9 Times in 7 Posts
Rep Power: 8 petectid is on a distinguished road
Default

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.
  Reply With Quote
Old 22-07-2008, 12:39 PM   #3
 
monkeyx's Avatar
 
Join Date: Nov 2006
Posts: 181
Thanks: 3
Thanked 13 Times in 11 Posts
Rep Power: 7 monkeyx will become famous soon enough
Default

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; 22-07-2008 at 12:46 PM..
  Reply With Quote
The Following User Says Thank You to monkeyx For This Useful Post:
Gatt (22-07-2008)
Old 22-07-2008, 12:46 PM   #4
 
Gatt's Avatar
 
Join Date: Jan 2006
Location: Moorside High, Swinton / Middleton, Rochdale
Posts: 2,135
uk uk scotland
Thanks: 80
Thanked 36 Times in 27 Posts
Rep Power: 19 Gatt is a jewel in the roughGatt is a jewel in the roughGatt is a jewel in the rough
Send a message via MSN to Gatt
Default

@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..
  Reply With Quote
Old 22-07-2008, 01:17 PM   #5
 
Gatt's Avatar
 
Join Date: Jan 2006
Location: Moorside High, Swinton / Middleton, Rochdale
Posts: 2,135
uk uk scotland
Thanks: 80
Thanked 36 Times in 27 Posts
Rep Power: 19 Gatt is a jewel in the roughGatt is a jewel in the roughGatt is a jewel in the rough
Send a message via MSN to Gatt
Default

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.
  Reply With Quote
Old 22-07-2008, 03:05 PM   #6
 
monkeyx's Avatar
 
Join Date: Nov 2006
Posts: 181
Thanks: 3
Thanked 13 Times in 11 Posts
Rep Power: 7 monkeyx will become famous soon enough
Default

Hmm a quick looks on google showed me this

Format single Row in datagridview using a value in same row - C#

Does that help?
  Reply With Quote
The Following User Says Thank You to monkeyx For This Useful Post:
Gatt (22-07-2008)
Old 22-07-2008, 08:33 PM   #7
 
Gatt's Avatar
 
Join Date: Jan 2006
Location: Moorside High, Swinton / Middleton, Rochdale
Posts: 2,135
uk uk scotland
Thanks: 80
Thanked 36 Times in 27 Posts
Rep Power: 19 Gatt is a jewel in the roughGatt is a jewel in the roughGatt is a jewel in the rough
Send a message via MSN to Gatt
Default

@monkeyx - this might very well!

I'll give it a bash and let you know!
  Reply With Quote
Reply

Register now for FREE and post messages!


Username: Password: Confirm Password: E-Mail: Confirm E-Mail:
Birthday:      
Image Verification
  I agree to forum rules 

Similar Threads
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
Search Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +1. The time now is 11:42 AM.
Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
Copyright EduGeek.net