This doesn't change the background colour - what am I doing wrong here?Code:dgv_WorkHistory.Rows(i).Cells("DateOfWork").Style.BackColor = Color.Green
Printable View
This doesn't change the background colour - what am I doing wrong here?Code:dgv_WorkHistory.Rows(i).Cells("DateOfWork").Style.BackColor = Color.Green
are you sure the column is named "DateOfWork"?
i tried it + it worked for me.
Yes, it's definitely the right column - I can change its value with something like :
but I can't change the background color of cells anywhere in the datagridview.Code:dgv_WorkHistory.Rows(i).Cells("DateOfWork").Value = "30/06/1971"
To change an individual cell you need to use .Style.BackColor
dgv[col, row].Style.BackColor = Color.Tomato
and to change a whole column you use .DefaultCellStyle.BackColor
dgv.Columns[col].DefaultCellStyle.BackColor = Color.Tomato
Sorry, this is c# syntax - I'm not sure if it's the same as vb but I'm sure you can work it out.
I'm already using .Style.Backcolor and it's not working for some reason.
I know in c# i have to be "using System.Drawing;" before I can use .Color otherwise it won't even build. Is VB strict or would it let you run without it ?
Try this?
dgv_WorkHistory.Rows(i).Cells("DateOfWork").Style.BackColor = System.Drawing.Color.Green
Thanks for continuing to help but no, that didn't work. TBH I think the intellisense would have alerted me if I was using Color.Green wrongly.
This is right, I've tested it. Its really strange that it doesnt work for you.
When I get super weird errors like this what I do is close the VS IDE and go to the application folder and delete the "bin" and "obj" folders. I then start the IDE again, reload the application and re-compile it.
I think the IDE has a couple of bugs when it comes to editing code and re-running it, you get weird **** where stuff that is supposed to work doesnt.
Did nothing, I'm afraid (assuming I did it correctly), but thanks for trying to help.
Here's where I am now:
These 2 lines follow one another in my code but only the second one works. That's significant, though, because it shows at least that the first line is pointing to the correct cell.Code:dgv_WorkHistory.Rows(i).Cells(1).Style.BackColor = Color.Green
dgv_WorkHistory.Rows(i).Cells(1).Value = "30/06/1971"
Nothing of any significance thus far (I think) - only this:-
The code I'm having trouble with is part of the routine which displays the dgv for the very first time. Here it is:-Code:Private Sub dgv_WorkHistory_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgv_WorkHistory.CellBeginEdit
Dim msg As String = String.Format("Editing Cell at ({0}, {1})", e.ColumnIndex, e.RowIndex)
Me.Text = msg
End Sub
Private Sub dgv_WorkHistory_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgv_WorkHistory.CellMouseClick
Dim msg As String = String.Format("Cell clicked on at ({0}, {1})", e.ColumnIndex, e.RowIndex)
Me.Text = msg
End Sub
Private Sub dgv_WorkHistory_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_WorkHistory.CellMouseEnter
If e.RowIndex < 0 Then
Exit Sub
End If
With dgv_WorkHistory.Rows(e.RowIndex).Cells(e.ColumnIndex)
.ToolTipText = String.Format("Mouse over cell at ({0}, {1})", e.ColumnIndex, e.RowIndex)
End With
End Sub
Private Sub cmdSaveChanges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveChanges.Click
db_UpdateWorkHistory(WorkHistory_tablename, ds_WorkHistory)
End Sub
Code:Private Sub Fill_dgv_WorkHistory(ByVal WorkHistory_tablename As String)
Dim i As Integer
ds_WorkHistory = db_GetWorkHistory(WorkHistory_tablename)
dgv_WorkHistory.DataSource = ds_WorkHistory.Tables(0).DefaultView
dgv_WorkHistory.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
dgv_WorkHistory.RowHeadersVisible = False
dgv_WorkHistory.Columns(0).Visible = False
dgv_WorkHistory.Columns(2).Visible = False
dgv_WorkHistory.Columns(4).Visible = False
dgv_WorkHistory.Columns(5).Visible = False
dgv_WorkHistory.Columns(12).Visible = False
dgv_WorkHistory.Columns(13).Visible = False
dgv_WorkHistory.Columns(14).Visible = False
dgv_WorkHistory.Columns(15).Visible = False
dgv_WorkHistory.Columns(1).Width = 67 'DateOfWork
dgv_WorkHistory.Columns(1).HeaderText = "Date"
dgv_WorkHistory.Columns(1).ReadOnly = True
dgv_WorkHistory.Columns(3).ReadOnly = True
dgv_WorkHistory.Columns(6).Width = 34 'Try
dgv_WorkHistory.Columns(6).HeaderText = "Tries"
dgv_WorkHistory.Columns(6).ReadOnly = True
dgv_WorkHistory.Columns(7).Width = 150 'TopicName
dgv_WorkHistory.Columns(7).HeaderText = "Topic"
dgv_WorkHistory.Columns(7).ReadOnly = True
dgv_WorkHistory.Columns(8).Width = 26 'NumSheets
dgv_WorkHistory.Columns(8).HeaderText = "Shts"
dgv_WorkHistory.Columns(8).ReadOnly = True
dgv_WorkHistory.Columns(9).Width = 25 'Errors
dgv_WorkHistory.Columns(9).HeaderText = "X"
dgv_WorkHistory.Columns(10).Width = 25 'Percentage
dgv_WorkHistory.Columns(10).HeaderText = "%"
dgv_WorkHistory.Columns(11).Width = 25 'Time
dgv_WorkHistory.Columns(11).HeaderText = "T"
cmdSaveChanges.Enabled = True
cmdSettingComplete.Enabled = True
Dim WorkDay As String
Dim DateValue As Date
Dim x As Integer
For i = 0 To dgv_WorkHistory.RowCount - 1
If CDate(dgv_WorkHistory.Rows(i).Cells("DateOfWork").Value) = Today Then
Todays_Row = i
End If
DateValue = CDate(dgv_WorkHistory.Rows(i).Cells("DateOfWork").Value)
WorkDay = DateValue.ToString("ddd")
If WorkDay = CurrentClassDay Then
dgv_WorkHistory.Rows(i).Cells(1).Style.BackColor = Color.Green
dgv_WorkHistory.Rows(i).Cells(1).Value = "30/06/1971"
If Todays_Row > 0 Then
If i >= Todays_Row And i < Todays_Row + 7 Then
dgv_WorkHistory.Rows(i).Cells(1).Style.BackColor = Color.Red
End If
Else
dgv_WorkHistory.Rows(0).Cells("DateOfWork").Style.BackColor = Color.Red
End If
End If
Next
End Sub
It looks like that column is filled with dates loaded from a Database.
Change:-
to something like:vbnet Code:
dgv_WorkHistory.Rows(i).Cells(1).Value = "30/06/1971"
So you can be absolutely 200% certain that you got the right cell and that the code is executing. There is no reason for that code not to work as far as I can see.vbnet Code:
dgv_WorkHistory.Rows(i).Cells(1).Value = "VERY DISTINCT VALUE"
Have you considered then using CellFormatting event to change the desired colors?
Try the following, see if it will work for you, requires a DataGridView on a form with the following code. A string and Date column are formatted in the CellFormat event.
Code:Public Class Form2
WithEvents bsData As New BindingSource
Private Sub DataGridView1_CellFormatting( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _
Handles DataGridView1.CellFormatting
If e.ColumnIndex = DataGridView1.Columns("Column2").Index Then
If e.Value.ToString = "2" Then
e.CellStyle.BackColor = Color.Crimson
e.CellStyle.ForeColor = Color.White
End If
End If
If e.ColumnIndex = DataGridView1.Columns("Column3").Index Then
If CType(e.Value, DateTime).Month > Now.Month Then
e.CellStyle.BackColor = Color.Green
e.CellStyle.ForeColor = Color.White
End If
End If
End Sub
Private Sub Form2_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
bsData.DataSource = MockData()
DataGridView1.AllowUserToAddRows = False
DataGridView1.DataSource = bsData
DataGridView1.Columns("Column3").DefaultCellStyle.Format = "d"
End Sub
Private Function MockData() As DataTable
Dim dt As New DataTable With {.TableName = "MyTable"}
dt.Columns.AddRange(New DataColumn() _
{ _
New DataColumn("Column1", GetType(System.String)), _
New DataColumn("Column2", GetType(System.String)), _
New DataColumn("Column3", GetType(System.DateTime)) _
} _
)
dt.Rows.Add(New Object() {"A", "1", Now})
dt.Rows.Add(New Object() {"B", "2", Now.AddMonths(1)})
dt.Rows.Add(New Object() {"C", "3", Now})
dt.Rows.Add(New Object() {"D", "2", Now.AddMonths(2)})
dt.Rows.Add(New Object() {"E", "5", Now})
dt.Rows.Add(New Object() {"F", "6", Now.AddMonths(3)})
dt.AcceptChanges()
Return dt
End Function
End Class
Brilliant - that did the trick! Many thanks!
Now the question arises : does the failure of my original code to work constitute a bug in VB.Net (given that the syntax was fine and intellisense didn't object to it) or is there an undocumented (or very well hidden) reason why VB won't allow it to work?
didn't see this option posted, so thought I'd add it ...
vb Code:
Dim QtyCell As DataGridViewCell Dim TraysCell As DataGridViewCell For i = 0 To fdgAddProducts.EmbeddedDataGridView.RowCount - 1 days2cellsize = fdgAddProducts.EmbeddedDataGridView.Rows(i).Cells("urc_daystocellsize").Value Dim stickDate As Date = DateAdd(DateInterval.Day, -days2cellsize, m_OrderShipDate) If Date.Today.Date > stickDate.Date Then 'if no time to grow plant we can't sell it 'color cells of product user can't sell QtyCell = fdgAddProducts.EmbeddedDataGridView.Rows(i).Cells("Quantity") QtyCell.Style.BackColor = Color.LightGray TraysCell = fdgAddProducts.EmbeddedDataGridView.Rows(i).Cells("Trays") TraysCell.Style.BackColor = Color.LightGray End If Next