+ Post New Thread
Results 1 to 7 of 7
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 ...
  1. #1

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,719
    Thank Post
    867
    Thanked 664 Times in 437 Posts
    Rep Power
    501

    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

  2. #2
    petectid's Avatar
    Join Date
    Jun 2005
    Posts
    298
    Thank Post
    2
    Thanked 15 Times in 13 Posts
    Rep Power
    20
    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. #3
    monkeyx's Avatar
    Join Date
    Nov 2006
    Posts
    364
    Thank Post
    8
    Thanked 52 Times in 41 Posts
    Rep Power
    26
    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.

  4. Thanks to monkeyx from:

    Gatt (22nd July 2008)

  5. #4

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,719
    Thank Post
    867
    Thanked 664 Times in 437 Posts
    Rep Power
    501
    @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.

  6. #5

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,719
    Thank Post
    867
    Thanked 664 Times in 437 Posts
    Rep Power
    501
    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.

  7. #6
    monkeyx's Avatar
    Join Date
    Nov 2006
    Posts
    364
    Thank Post
    8
    Thanked 52 Times in 41 Posts
    Rep Power
    26
    Hmm a quick looks on google showed me this

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

    Does that help?

  8. Thanks to monkeyx from:

    Gatt (22nd July 2008)

  9. #7

    Gatt's Avatar
    Join Date
    Jan 2006
    Posts
    6,719
    Thank Post
    867
    Thanked 664 Times in 437 Posts
    Rep Power
    501
    @monkeyx - this might very well!

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

SHARE:
+ Post New Thread

Similar Threads

  1. Cell Phone signal.
    By laserblazer in forum General Chat
    Replies: 10
    Last Post: 27th June 2008, 09:12 AM
  2. Conditional formatting help needed
    By Jobos in forum How do you do....it?
    Replies: 3
    Last Post: 3rd June 2008, 01:58 PM
  3. Help with conditional formatting
    By randle in forum Windows
    Replies: 3
    Last Post: 25th October 2007, 01:20 PM
  4. Excel Cell colour changes
    By wesleyw in forum How do you do....it?
    Replies: 3
    Last Post: 20th December 2006, 10:01 PM
  5. formatting w98
    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
  •