Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Can anyone help me with some code snipet about this in 1 column i need to highlite the cells witch have same value or same text (duplicated text or value)
i found this but it is on rows i don't need on rows i need on column cells
here it is
C#
<pre lang="C#">
for (int currentRow = 0; currentRow &lt; dataGridView1.Rows.Count - 1; currentRow++)
{
DataGridViewRow rowToCompare = dataGridView1.Rows[currentRow];

for (int otherRow = currentRow + 1; otherRow &lt; dataGridView1.Rows.Count; otherRow++)
{
DataGridViewRow row = dataGridView1.Rows[otherRow];

bool duplicateRow = true;

if (!rowToCompare.Cells[4].Value.Equals(row.Cells[4].Value))
{
duplicateRow = false;
break;
}

if (duplicateRow)
{
rowToCompare.DefaultCellStyle.BackColor = Color.Red;
rowToCompare.DefaultCellStyle.ForeColor = Color.Black;
row.DefaultCellStyle.BackColor = Color.Red;
row.DefaultCellStyle.ForeColor = Color.Black;
}
}
}</pre>
Posted
Updated 1-Feb-16 0:29am
v2
Comments
dan!sh 1-Feb-16 6:20am    
You might want to search web or try writing it yourself first.
dan!sh 1-Feb-16 6:25am    
This should have been a part of your question text. How do we know what all you have done if you don't post it? Update your question with the code and tell what is not working out.
Chris Ross 2 1-Feb-16 6:34am    
If you're asking whether anyone can help you with the problem, the short answer is clearly no :) SOMEone can, but definitely not ANYone. But asking about the state of the world is a question that belongs in the lounge, not in quick answers. (Being a linguistic pedant I'd recommend, if you actually want help, rephrasing your question: "Would someone who can, please help me..." - but to suggest you do that would be rude, so I won't).

As to actual help:

You need to break your problem down into its constituent parts: (1) identifying a column that contains two or more cells with identical values, and (2) how to hilight the column (and how to remove the hilight if a column no longer has duplicates).

With which part are you having difficulties, and what have you tried so far? (I see what your replied to D@nish of what you found online being about rows ... but what have you actually tried?)
GTR0123 1-Feb-16 6:46am    
i tried and i can't do so i post here so if you just don't correct me and if you know the answer about this you can answer if you don't just don't correct me ok?
Sinisa Hajnal 1-Feb-16 7:08am    
This is rude and isn't making you any friends.
The question is easily solved since you already have sample code. Adjustment to include columns is a small one and you're not really showing any effort to solve it. By your own words you found the sample above on the net and didn't even try to modify it.

1 solution

OK, Since I had fun with my linguistic pedantry and feel a bit guilty for it, here is a solution to what I think the problem is. It assumes a form with a (WinForms) DataGridView loaded; it highlights the whole column if the column contains any cells with duplicate data. It's a test app so create a new project, add a DataGridView to the form, pull the relevant parts from the code below and play with it:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DGV_Tester
{
	public partial class Form1 : Form
	{
		DataGridViewCellStyle m_hilightStyle = null;

		public Form1()
		{
			InitializeComponent();
			SetHilightCellStyle();
		}

		private void SetHilightCellStyle()
		{
			m_hilightStyle = dataGridView1.DefaultCellStyle.Clone();
			m_hilightStyle.ForeColor = Color.Red;
			m_hilightStyle.BackColor = Color.Yellow;
		}

		private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			ValidateGrid();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			dataGridView1.DataSource = MyData.Values;
			ValidateGrid();
		}

		private void ValidateGrid()
		{
			foreach (var colNum in Enumerable.Range(0, dataGridView1.Columns.Count))
			{
				bool matchFound = false;
				string keyValue = null;

				// Row counting math: say we have two rows - RowCount = 3; Range() yields three values - 0, 1 and two
				// targetRowNum range must be '1 for two rows, 2 for one row and 3 for no rows', 
				// i.e. the Range() count must be RowCount - 1 - rowNum
				foreach (var rowNum in Enumerable.Range(0, dataGridView1.RowCount))
				{
					keyValue = dataGridView1.Rows[rowNum].Cells[colNum].Value.ToString();
					foreach (var targetRowNum in Enumerable.Range(rowNum + 1, dataGridView1.RowCount - 1 - rowNum))
					
					if (keyValue == dataGridView1.Rows[targetRowNum].Cells[colNum].Value.ToString())
					{
						matchFound = true;
						break;
					}
				}
				HighlightColumn(colNum, matchFound);
			}
		}

		private void HighlightColumn(int colNum, bool matchFound)
		{
			dataGridView1.Columns[colNum].DefaultCellStyle = matchFound ? m_hilightStyle : dataGridView1.DefaultCellStyle;
		}
	}
}
 
Share this answer
 
Comments
GTR0123 1-Feb-16 7:36am    
Ty that is better thnx again working i changed 1 thing and it is working good

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900