Solved

compare values on two colums on datagridview

Posted on 2013-12-05
6
324 Views
Last Modified: 2014-01-23
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_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView2.CellFormatting
        If Me.DataGridView2.Columns(e.ColumnIndex).Name = "OLDPRICE" Then
            If e.Value IsNot Nothing Then
                XOLPPRICE = e.Value.ToString
            End If
        End If
        If Me.DataGridView2.Columns(e.ColumnIndex).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
0
Comment
Question by:teogos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 39700311
hi.. please try this code.

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,fname,grno from students where classname='" & Me.ComboBox2.Text & "'"
           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", "Fee Month")
       DGV2.Columns.Add("status", "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").HeaderCell.Value = ("Class")
               .Columns("studentname").HeaderCell.Value = ("Student Name")
               .Columns("fname").HeaderCell.Value = ("Father's Name")
               .Columns("grno").HeaderCell.Value = ("GR NO")
 
           End With
 
           If DGV1.Rows.Count > 0 Then
 
           End If
 

 

 

           If DGV1.Rows.Count = 0 Then
               DGV2.Rows(i).Cells("status").Value = "Un Paid"
               DGV2.Rows(i).Cells("feemonth").Value = Me.ComboBox1.Text
               DGV2.Rows(i).Cells("status").Style.ForeColor = Color.Red
 

 
           ElseIf DGV1.CurrentRow.Cells("name").Value.ToString = DGV2.Rows(i).Cells("studentname").Value.ToString Then
 

               DGV2.Rows(i).Cells("status").Value = "Paid"
               DGV2.Rows(i).Cells("status").Style.ForeColor = Color.Blue
               DGV2.Rows(i).Cells("feemonth").Value = Me.ComboBox1.Text
           Else
               DGV2.Rows(i).Cells("status").Value = "UN Paid"
               DGV2.Rows(i).Cells("status").Style.ForeColor = Color.Red
 
               DGV2.Rows(i).Cells("feemonth").Value = Me.ComboBox1.Text
           End If
 

       Next
 
       DataR.Close()
 
   End Sub
0
 

Author Comment

by:teogos
ID: 39701328
I need to compare values on the same DATAGRIDVIEW
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39701739
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
0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

Author Comment

by:teogos
ID: 39803651
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
0
 

Author Comment

by:teogos
ID: 39803131
Job is not longer feasible
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 39803483
Answer was provided.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question