Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Compare rows of a datagridview and remove repeated rows

Compare rows of a datagridview and remove repeated rows

Scheduled Pinned Locked Moved C#
help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GREG_DORIANcod
    wrote on last edited by
    #1

    Hi there! I am trying to compare the rows of a Datagridview and that it removes the rows that are repeated. I think that I´m doing something wrong, need some of help!! my mind is not clear today! (maybe x-mas holyday! ;) ) here´s the code:

        public void Compare(DataGridView grv)
        { 
         grv.Sort(grv.Columns\[0\],ListSortDirection.Ascending);
         for ( int row = 0; row < grv.Rows.Count; row++)
         {
         for ( int col = 0; col < grv.Columns.Count; col++)
         {
             int rowx=1;
             if (grv.Rows\[row\].Cells\[col\].Value != null && grv.Rows\[row\].Cells\[col\].Value.Equals(grv.Rows\[rowx\].Cells\[col\].Value))
             {
                 if (col == grv.Columns.Count - 1)
                 {
                     grv.Rows.RemoveAt(row);
                     grv.Sort(grv.Columns\[0\], ListSortDirection.Descending);
                 }
             }
             else
             {
                 grv.FirstDisplayedScrollingRowIndex = grv.RowCount - 1;
                 grv.Rows\[grv.RowCount - 1\].Selected = true;
             }
            }
         }
        }
    
    U 1 Reply Last reply
    0
    • G GREG_DORIANcod

      Hi there! I am trying to compare the rows of a Datagridview and that it removes the rows that are repeated. I think that I´m doing something wrong, need some of help!! my mind is not clear today! (maybe x-mas holyday! ;) ) here´s the code:

          public void Compare(DataGridView grv)
          { 
           grv.Sort(grv.Columns\[0\],ListSortDirection.Ascending);
           for ( int row = 0; row < grv.Rows.Count; row++)
           {
           for ( int col = 0; col < grv.Columns.Count; col++)
           {
               int rowx=1;
               if (grv.Rows\[row\].Cells\[col\].Value != null && grv.Rows\[row\].Cells\[col\].Value.Equals(grv.Rows\[rowx\].Cells\[col\].Value))
               {
                   if (col == grv.Columns.Count - 1)
                   {
                       grv.Rows.RemoveAt(row);
                       grv.Sort(grv.Columns\[0\], ListSortDirection.Descending);
                   }
               }
               else
               {
                   grv.FirstDisplayedScrollingRowIndex = grv.RowCount - 1;
                   grv.Rows\[grv.RowCount - 1\].Selected = true;
               }
              }
           }
          }
      
      U Offline
      U Offline
      User 9609290
      wrote on last edited by
      #2

      hi, you forgot the a counter if all cols in a row are equal to the ones in rowx? in this case you would delete the row always if the last col is equal. Maybe thats it? cu

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups