This is a migrated thread and some comments may be shown as answers.

Finding duplicates in cells and changing ForeColor

1 Answer 355 Views
GridView
This is a migrated thread and some comments may be shown as answers.
superold
Top achievements
Rank 1
superold asked on 03 Jun 2009, 10:20 PM
Hi,

I need to highlight duplicates in a grid. I am using CellFormatting to validate data, probably should do it somewhere else?. The grid is unbound. 

How do I find the duplicate and change the appearance of it? This seems to be a rather bad way to do this. 

private void mygrid_CellFormatting(object sender, CellFormattingEventArgs e) 
            if (e.CellElement.ColumnInfo is GridViewDataColumn && !(e.CellElement.RowElement is GridTableHeaderRowElement)) 
            { 
                if (e.CellElement.ColumnIndex == userEmailColumn.Index) 
                { 
                    string emailValue = e.CellElement.RowInfo.Cells[userEmailColumn.Index].Value as string
 
                    if (!String.IsNullOrEmpty(emailValue)) 
                    { 
                        Match match = emailRegexValidation.Match(emailValue); 
 
                        if (!((match.Success && (match.Index == 0)) && (match.Length == emailValue.Length))) 
                        { 
                            e.CellElement.ForeColor = Color.Red; 
                            e.CellElement.ToolTipText = "E-mail is invalid"
                            valid = false
                        } 
 
                        // now look if there are any duplicates and mark them all



 
 
// Loop through all cells and find the duplicates?

Thanks,
/ jorge

1 Answer, 1 is accepted

Sort by
0
Victor
Telerik team
answered on 05 Jun 2009, 07:30 AM
Hi Jorge,

Thank you for writing. CellFormatting event is the correct event to change the colors of cells. There is no easy way to find duplicate values in the cells. You have to write a loop and check each cell. Here is a way you can achieve that:

private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)  
{  
    // Reset the color of the cell.  
    e.CellElement.DrawFill = false;  
    // Example column index. Should be the cell you need to check for identical values.  
    int colIndex = 1;  
 
    for (int i = 0; i < this.radGridView1.Rows.Count; i++)  
    {  
        GridViewCellInfo celli = this.radGridView1.Rows[i].Cells[colIndex];  
        for (int j = 0; j < this.radGridView1.Rows.Count; j++)  
        {  
            GridViewCellInfo cellj = this.radGridView1.Rows[j].Cells[colIndex];  
            if (celli.Value.ToString() == cellj.Value.ToString() && i != j)  
            {  
                HighlightCell(celli);  
                HighlightCell(cellj);  
            }  
        }  
    }  
}  
 
private void HighlightCell(GridViewCellInfo cell)  
{  
    if (cell.CellElement != null && cell.CellElement.DrawFill == false)  
    {  
        cell.CellElement.GradientStyle = GradientStyles.Solid;  
        cell.CellElement.BackColor = Color.Red;  
        cell.CellElement.DrawFill = true;  
    }  

Please write back if you need further assistance.

Greetings,
Victor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
superold
Top achievements
Rank 1
Answers by
Victor
Telerik team
Share this question
or