如果存在重复的单元格,则突出显示datagridview列
问题描述:
任何人都可以帮助我在1列中使用一些代码snipet我需要高亮显示具有相同值或相同文本(重复文本或值)的单元格
i找到了这个但是它在行上我不需要在列单元格上需要的行
这里它是
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
<pre lang="C#">
for (int currentRow = 0; currentRow < dataGridView1.Rows.Count - 1; currentRow++)
{
DataGridViewRow rowToCompare = dataGridView1.Rows[currentRow];
for (int otherRow = currentRow + 1; otherRow < 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>
答
好的,因为我和我的语言学家一起玩得很开心有点内疚,这是我认为问题的解决方案。它假定一个加载了(WinForms)DataGridView的表单;如果列包含任何具有重复数据的单元格,它会突出显示整列。这是一个测试应用程序,所以创建一个新项目,向表单添加一个DataGridView,从下面的代码中拉出相关的部分并使用它:
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:
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;
}
}
}