-
Oct 24th, 2012, 04:12 PM
#1
Thread Starter
New Member
Error changing indvidual cells in a datagridview
I am trying to change the color of individual cells in a datagridview based on values from the columns. If the CDRGvalue is not blank and the ALOSvalue is greater than the PLOSvalue then I need to change the individual cell back color for the ALOS column (colALOS) and the PLOS column (colPLOS) to red for the patient in that row. Currently, the best I have been able to do is turn the whole line red instead of just the individual cells. Any help, tips, or suggestions are greatly appreciated!
Code:
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
Dim ALOSValue As Double
Dim PLOSValue As Double
Dim CDRGValue As String
For Each tempRow As System.Windows.Forms.DataGridViewRow In Me.DataGridView1.Rows
For Each tempCell As Windows.Forms.DataGridViewCell In tempRow.Cells
ALOSValue = Val(tempRow.Cells.Item("colALOS").Value)
PLOSValue = Val(tempRow.Cells.Item("colPLOS").Value)
CDRGValue = tempRow.Cells.Item("colDRG").Value
If CDRGValue <> "" Then
If (ALOSValue > PLOSValue) Then
tempCell.Style.BackColor = Color.Red
End If
End If
Next
Next
End Sub
-
Oct 24th, 2012, 04:59 PM
#2
Re: Error changing indvidual cells in a datagridview
Looks like you're making it more complicated than it need be. Basically it's ...
DataGridView1.Rows(index1).Cells(index2).Style.BackColor = Color.Red
... so you need to use indexed loops rather than For Each.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Oct 24th, 2012, 11:13 PM
#3
Re: Error changing indvidual cells in a datagridview
You're changing the whole row colour because you're looping through the Cells of the row. If you don't want to do something to every cell then don't loop through all the cells. You can simply get rid of the inner loop. A For Each loop is fine for the Rows but, for each row, you just want to manipulate two specific cells, not do soemthing for each cell in the row.
Code:
For Each row As DataGridViewRow In myDataGridView.Rows
Dim aCell As DataGridViewCell = row.Cells("A")
Dim bCell As DataGridViewCell = row.Cells("B")
'Test the value and set the style of aCell here.
'Test the value and set the style of bCell here.
Next
Tags for this Thread
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
|