Compare 2 Gridview row values and write those who doesn't match

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • parshupooja
    New Member
    • Jun 2007
    • 159

    Compare 2 Gridview row values and write those who doesn't match

    Hey All,

    I have 2 Gridviews. forex:
    Gridview 1
    One
    Two
    Three
    Four
    Gridview 2
    One
    Two
    Three
    Four
    five
    six

    both are returning one column and many rows. I want to compare values of both Gridviews and present result out those who does not match. for ex: in this case it will be
    five
    six

    any idea?
    thanks
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    The simplest way is to iterate through the DataRows...this can become quite costly the more data the GridViews hold. I'm assuming you want to extract any items that don't appear in both tables. The dictionary object will probably help as you can use the .Exists method to determine if an item exists without having to iterate the dictionary. Maybe convert the data of each GridView into a dictionary first? There are dozens of approaches you could use. It's probably one of those areas where any answer is the right one... performance is the only issue of concern here.

    Comment

    • parshupooja
      New Member
      • Jun 2007
      • 159

      #3
      yes u r right I want extract any items that don't appear in both tables. I am not aware of dictionary object. could gimme an example?

      Thanks

      Originally posted by balabaster
      The simplest way is to iterate through the DataRows...this can become quite costly the more data the GridViews hold. I'm assuming you want to extract any items that don't appear in both tables. The dictionary object will probably help as you can use the .Exists method to determine if an item exists without having to iterate the dictionary. Maybe convert the data of each GridView into a dictionary first? There are dozens of approaches you could use. It's probably one of those areas where any answer is the right one... performance is the only issue of concern here.

      Comment

      • parshupooja
        New Member
        • Jun 2007
        • 159

        #4
        I am using asp.net C# and I don't think there is dictionary object replacement in .net?

        Originally posted by balabaster
        The simplest way is to iterate through the DataRows...this can become quite costly the more data the GridViews hold. I'm assuming you want to extract any items that don't appear in both tables. The dictionary object will probably help as you can use the .Exists method to determine if an item exists without having to iterate the dictionary. Maybe convert the data of each GridView into a dictionary first? There are dozens of approaches you could use. It's probably one of those areas where any answer is the right one... performance is the only issue of concern here.
        Last edited by parshupooja; Mar 21 '08, 06:45 PM. Reason: err

        Comment

        • balabaster
          Recognized Expert Contributor
          • Mar 2007
          • 798

          #5
          Actually, having given it a little thought, I'd probably do something like this:
          VB
          Code:
          Dim oAry1 As New ArrayList(New Integer() {1, 2, 3, 5, 7, 10})
          Dim oAry2 As New ArrayList(New Integer() {1, 2, 4, 6, 8, 10})
          Dim oAry3 As New ArrayList()
          For Each i As Integer In oAry1
            If Not oAry2.Contains(i) Then oAry3.Add(i)
          Next
          For Each i As Integer In oAry2
            If Not oAry1.Contains(i) Then oAry3.Add(i)
          Next
          oAry3.Sort()
          MsgBox(Join(oAry3.ToArray, ","))
          Now all you have to figure out is how to get your data into two ArrayLists ;o)
          C#
          Code:
          ArrayList oAry1 = new ArrayList(new int[] {1, 2, 3, 5, 7});
          ArrayList oAry2 = new ArrayList(new int[] {1, 2, 4, 6, 8, 10});
          ArrayList oAry3 = new ArrayList();
          foreach (int i in oAry1){
            if (!oAry2.Contains(i))
          	oAry3.Add(i);
          }
          foreach (int i in oAry2){
            if (!oAry1.Contains(i))
          	oAry3.Add(i);
          }
          oAry3.Sort();

          Comment

          • parshupooja
            New Member
            • Jun 2007
            • 159

            #6
            Thanks for reply,
            I am using asp.net c#. How shd I save stored procedure results to array list?
            write now i am displaying in gridviews like this
            GridView1.DataS ource = command1.Execut eReader(Command Behavior.CloseC onnection);
            GridView1.DataB ind();

            GridView2.DataS ource = command2.Execut eReader(Command Behavior.CloseC onnection);
            GridView2.DataB ind();

            I need to compare values between this two gridviews and list those are not present in both

            thanks again
            Originally posted by balabaster
            Actually, having given it a little thought, I'd probably do something like this:
            VB
            Code:
            Dim oAry1 As New ArrayList(New Integer() {1, 2, 3, 5, 7, 10})
            Dim oAry2 As New ArrayList(New Integer() {1, 2, 4, 6, 8, 10})
            Dim oAry3 As New ArrayList()
            For Each i As Integer In oAry1
              If Not oAry2.Contains(i) Then oAry3.Add(i)
            Next
            For Each i As Integer In oAry2
              If Not oAry1.Contains(i) Then oAry3.Add(i)
            Next
            oAry3.Sort()
            MsgBox(Join(oAry3.ToArray, ","))
            Now all you have to figure out is how to get your data into two ArrayLists ;o)
            C#
            Code:
            ArrayList oAry1 = new ArrayList(new int[] {1, 2, 3, 5, 7});
            ArrayList oAry2 = new ArrayList(new int[] {1, 2, 4, 6, 8, 10});
            ArrayList oAry3 = new ArrayList();
            foreach (int i in oAry1){
              if (!oAry2.Contains(i))
            	oAry3.Add(i);
            }
            foreach (int i in oAry2){
              if (!oAry1.Contains(i))
            	oAry3.Add(i);
            }
            oAry3.Sort();

            Comment

            • parshupooja
              New Member
              • Jun 2007
              • 159

              #7
              thanks it worked. :)
              Originally posted by balabaster
              Actually, having given it a little thought, I'd probably do something like this:
              VB
              Code:
              Dim oAry1 As New ArrayList(New Integer() {1, 2, 3, 5, 7, 10})
              Dim oAry2 As New ArrayList(New Integer() {1, 2, 4, 6, 8, 10})
              Dim oAry3 As New ArrayList()
              For Each i As Integer In oAry1
                If Not oAry2.Contains(i) Then oAry3.Add(i)
              Next
              For Each i As Integer In oAry2
                If Not oAry1.Contains(i) Then oAry3.Add(i)
              Next
              oAry3.Sort()
              MsgBox(Join(oAry3.ToArray, ","))
              Now all you have to figure out is how to get your data into two ArrayLists ;o)
              C#
              Code:
              ArrayList oAry1 = new ArrayList(new int[] {1, 2, 3, 5, 7});
              ArrayList oAry2 = new ArrayList(new int[] {1, 2, 4, 6, 8, 10});
              ArrayList oAry3 = new ArrayList();
              foreach (int i in oAry1){
                if (!oAry2.Contains(i))
              	oAry3.Add(i);
              }
              foreach (int i in oAry2){
                if (!oAry1.Contains(i))
              	oAry3.Add(i);
              }
              oAry3.Sort();

              Comment

              Working...