Results 1 to 5 of 5

Thread: DataGridView back colour won't change...?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    201

    DataGridView back colour won't change...?

    Afternoon all,

    I have a datagridview that is populated with data from a SQL database. I'm trying to change the back colour depandant on the contents of the cell. I can't seem to get it to work even though its looping through and correctly picking up the data contained. Even my hard-coded back colour change isn't working... no errors, it just doesn't change colour.... is there anything I'm doing wrong...?

    Code:
    TestDGV.Item(5, 0).Style.BackColor = Color.Green
    This should change the Column 5 Row 0 cell to green...?!

    Thanks

    Dave
    Last edited by QuattroDave; Aug 1st, 2016 at 09:32 AM. Reason: typo

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: DataGridView back colour won't change...?

    Take a look at DataGridView's CellFormatting Event here for painting the cell.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    201

    Re: DataGridView back colour won't change...?

    Hi KGComputers,

    The cell formatting event works fine for the first part. I'm still really confused as to why I can't set one single cell colour manually.

    Code:
    TestDGV.Columns(6).DefaultCellStyle.BackColor = Color.Red
    Background colour of column 6 is as expected red.

    Code:
    TestDGV.Rows(6).DefaultCellStyle.BackColor = Color.Red
    Background colour of row 6 is NOT changed to red.

    Code:
    TestDGV.Item(5, 5).Value = "!"
    Cell at column 5 & row 5 has as expected a '!' in it.

    Code:
    TestDGV.Item(5, 5).Style.BackColor = Color.Red
    Cell at column 5 & row 5 is NOT changed to red

    Really not understanding why something that should be simple doesn't appear to be working, is it that the datagridview is bound to a datatable...??!?

    Thanks

    Dave
    Last edited by QuattroDave; Aug 2nd, 2016 at 02:51 AM. Reason: typo

  4. #4
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: DataGridView back colour won't change...?

    I normally use this code to change the back color of cell manually.
    vb.net Code:
    1. dgvSchedulesAndTasks.Rows(0).Cells(4).Style.BackColor = Color.Red

    And put this after data binding is completed.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    201

    Re: DataGridView back colour won't change...?

    Quote Originally Posted by KGComputers View Post
    after data binding is completed.
    This phrase sparked a sudden thought... I've come accross this before... After digging through some old code, there's a data binding complete event...!


    Code:
    Private Sub TestDGV_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles TestDGV.DataBindingComplete
            
            ' Once databinding is complete change test cell to red.
            TestDGV.Item(5, 5).Style.BackColor = Color.Red
    
    End Sub
    And..... it works :-D

    Great job, thanks KGC...!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width