-
Aug 1st, 2016, 09:31 AM
#1
Thread Starter
Addicted Member
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
-
Aug 1st, 2016, 11:13 AM
#2
Re: DataGridView back colour won't change...?
Take a look at DataGridView's CellFormatting Event here for painting the cell.
-
Aug 1st, 2016, 01:09 PM
#3
Thread Starter
Addicted Member
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
-
Aug 1st, 2016, 02:09 PM
#4
Re: DataGridView back colour won't change...?
I normally use this code to change the back color of cell manually.
vb.net Code:
dgvSchedulesAndTasks.Rows(0).Cells(4).Style.BackColor = Color.Red
And put this after data binding is completed.
-
Aug 2nd, 2016, 02:50 AM
#5
Thread Starter
Addicted Member
Re: DataGridView back colour won't change...?
Originally Posted by KGComputers
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|