Search code examples
c#devexpressspreadsheet

Removing duplicate values in a column in DevEpress Spreadsheet


DevEpress Spreadsheet doesn't currently support "Remove Duplicate" function. I want to write a C# code to do this manually. I have a column of values. Some of them are duplicate and those duplicate values may or may not be adjacent. I want to remove corresponding row of duplicate values. I tried this code:

IWorkbook workbook = spreadsheetControl.Document;
        Worksheet worksheet = workbook.Worksheets["Sheet1"];
        CellRange range = worksheet.GetUsedRange();
        int LastRow = range.BottomRowIndex;
        //MessageBox.Show(Convert.ToString(LastRow));
        for (int i = 0; i < LastRow; i++)
        {
            for (int j = i+1; j < LastRow; j++)
            {
                if (worksheet.Cells[i,0].Value == worksheet.Cells[j,0].Value)
                {
                    worksheet.Rows[j].Delete();
                }
            }
        }

It doesn't work properly.


Solution

  • I found a solution:

      IWorkbook workbook = spreadsheetControl.Document;
            Worksheet worksheet = workbook.Worksheets["Sheet1"];
            CellRange range = worksheet.GetUsedRange();
            int LastRow = range.BottomRowIndex;
            //MessageBox.Show(Convert.ToString(LastRow));
            //Let's keep track of every value seen as we go through the rows
            var valuesSeen = new HashSet<CellValue>();
            //Rows marked for deletion
            var duplicateRows = new List<Row>();
            for (int i = 0; i < LastRow+1; i++)
            {
                string cellValue = worksheet.Cells[i, 0].DisplayText;
                //Returns false if the value already exists
                if (valuesSeen.Add(cellValue)==false)
                {
                    //Mark this row for deletion since we've seen the value before
                    duplicateRows.Add(worksheet.Rows[i]);
                }
            }
            //Delete all marked rows only after we're done identifying them.
            for (int index = duplicateRows.Count-1; index >= 0; index--)
            {
                Row item = duplicateRows[index];
                item.Delete();
            }