teogos
asked on
compare values on two colums on datagridview
I have a datagridview populated
I want to compare the values on each row on two columns and if they are different then change the forecolor on one column
I am using this kind of works but look like not all the rows are compare
Private Sub DataGridView2_CellFormatti ng(sender As Object, e As System.Windows.Forms.DataG ridViewCel lFormattin gEventArgs ) Handles DataGridView2.CellFormatti ng
If Me.DataGridView2.Columns(e .ColumnInd ex).Name = "OLDPRICE" Then
If e.Value IsNot Nothing Then
XOLPPRICE = e.Value.ToString
End If
End If
If Me.DataGridView2.Columns(e .ColumnInd ex).Name = "NEWPRICE" Then
If e.Value IsNot Nothing Then
XNEWPRICE = e.Value.ToString
RESULT = String.Compare(XOLPPRICE, XNEWPRICE)
If RESULT = True Then
e.CellStyle.ForeColor = Color.Red
Else
e.CellStyle.ForeColor = Color.Green
End If
End If
End If
End Sub
the values on the columns are like thise
OLDPRICE NEWPRICE
2.45 2.50
3.34 3.34
so if the value on NEWPRICE is different the OLDPRICE I want NEWPRICE to be color red
I want to compare the values on each row on two columns and if they are different then change the forecolor on one column
I am using this kind of works but look like not all the rows are compare
Private Sub DataGridView2_CellFormatti
If Me.DataGridView2.Columns(e
If e.Value IsNot Nothing Then
XOLPPRICE = e.Value.ToString
End If
End If
If Me.DataGridView2.Columns(e
If e.Value IsNot Nothing Then
XNEWPRICE = e.Value.ToString
RESULT = String.Compare(XOLPPRICE, XNEWPRICE)
If RESULT = True Then
e.CellStyle.ForeColor = Color.Red
Else
e.CellStyle.ForeColor = Color.Green
End If
End If
End If
End Sub
the values on the columns are like thise
OLDPRICE NEWPRICE
2.45 2.50
3.34 3.34
so if the value on NEWPRICE is different the OLDPRICE I want NEWPRICE to be color red
ASKER
I need to compare values on the same DATAGRIDVIEW
I would personally handle this during the Itemdatabound event.
But, in the 2nd part of your code for the new price you're only setting the color if new price is not nothing . . . what happens if / when old price or new price is null?
If e.Value IsNot Nothing Then
XNEWPRICE = e.Value.ToString
'XOLPRICE MAY NOT HAVE A VALUE BASED ON CODE IN ORIGINAL POST.
RESULT = String.Compare(XOLPPRICE, XNEWPRICE)
If RESULT = True Then
e.CellStyle.ForeColor = Color.Red
Else
e.CellStyle.ForeColor = Color.Green
End If
else
'do something.
End If
But, in the 2nd part of your code for the new price you're only setting the color if new price is not nothing . . . what happens if / when old price or new price is null?
If e.Value IsNot Nothing Then
XNEWPRICE = e.Value.ToString
'XOLPRICE MAY NOT HAVE A VALUE BASED ON CODE IN ORIGINAL POST.
RESULT = String.Compare(XOLPPRICE, XNEWPRICE)
If RESULT = True Then
e.CellStyle.ForeColor = Color.Red
Else
e.CellStyle.ForeColor = Color.Green
End If
else
'do something.
End If
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for teogos's comment #a39701328
for the following reason:
Job not on active status
Accepted answer: 0 points for teogos's comment #a39701328
for the following reason:
Job not on active status
ASKER
Job is not longer feasible
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Private Sub btncheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncheck.Click
Dim mytable As New DataTable
CMD.Connection = CN
CMD.CommandText = "Select classname,studentname,fnam
DataR = CMD.ExecuteReader
Dim i As Integer
For i = 0 To mytable.Rows.Count - 1
DataR.Read()
Next
mytable.Load(DataR)
DGV2.DataSource = mytable
DGV2.Columns.Add("feemonth
DGV2.Columns.Add("status",
For i = 0 To mytable.Rows.Count - 1
Dim i2 As Integer
For i2 = 0 To DGV2.Rows.Count - 1
Next
With DGV2
.Columns("classname").Head
.Columns("studentname").He
.Columns("fname").HeaderCe
.Columns("grno").HeaderCel
End With
If DGV1.Rows.Count > 0 Then
End If
If DGV1.Rows.Count = 0 Then
DGV2.Rows(i).Cells("status
DGV2.Rows(i).Cells("feemon
DGV2.Rows(i).Cells("status
ElseIf DGV1.CurrentRow.Cells("nam
DGV2.Rows(i).Cells("status
DGV2.Rows(i).Cells("status
DGV2.Rows(i).Cells("feemon
Else
DGV2.Rows(i).Cells("status
DGV2.Rows(i).Cells("status
DGV2.Rows(i).Cells("feemon
End If
Next
DataR.Close()
End Sub