Compare rows of a datagridview and remove repeated rows
C#
2
Posts
2
Posters
0
Views
1
Watching
-
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; } } } }
-
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; } } } }
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