如何在datagridview中突出显示单元格? [英] How can i highlight the cell in datagridview?
本文介绍了如何在datagridview中突出显示单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个包含datagridview的小应用程序,此gridview由文本文件填充。现在,如果我在任何单元格中进行了一些更改或添加了一些新单元格,那么它应该以某种颜色突出显示,直到我不保存此更改。
我使用了
I am creating a small application that contains a datagridview and this gridview is filled by text file. Now If I made some changes in any cell or add some new cells then it should be highlighted by some color untill I am not save this changes.
I have used the
dataGridView1_CellValidating
事件,它运行正常。但问题是,一旦我更改了单元格的值,它就会突出显示,但如果我恢复原始值,它仍然会保持突出显示不应该。
我使用以下代码进行比较:
event and it works fine. but the problem is that once I changed the value of cell it gets highlighted but if I restore the original value it still remains highlighted that should not.
I have use following code for comparing:
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
var oldValue = dataGridView1[e.ColumnIndex, e.RowIndex].Value;
var newValue = e.FormattedValue;
if (oldValue != newValue)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = System.Drawing.Color.LightPink;
}
}
}
}
推荐答案
使用DataGridViewCell.Tag属性 - 将其设置为 bool
值,并在 DataGridView.CellPainting Event(System.Windows.Forms) [ ^ ] - 如果是真的,它会改变,需要一个不同的背景或你选择的任何东西。
Use the DataGridViewCell.Tag property - set it to a bool
value and check it in the DataGridView.CellPainting Event (System.Windows.Forms)[^] - if it's true, it's changed and needs a different background or whatever you choose.
这篇关于如何在datagridview中突出显示单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文