如何检测单元格值已更改datagridview c#

如何检测单元格值已更改datagridview c#,c#,.net,datagridview,C#,.net,Datagridview,关于SOF的类似问题似乎没有一个明确的答案 我有一个绑定到BindingList对象的DataGridView(这是一个自定义对象列表;还继承了INotifyPropertyChanged)。每个自定义对象都有一个唯一的计时器。当这些计时器通过某个值(比如10秒)时,我想将单元格的前景色改为红色 我正在使用CellValueChanged事件,但此事件似乎从未触发,即使我可以在DataGridView上看到计时器正在更改。有没有其他我应该寻找的活动?下面是我的CellValueChanged处理

关于SOF的类似问题似乎没有一个明确的答案

我有一个绑定到
BindingList
对象的
DataGridView
(这是一个自定义对象列表;还继承了
INotifyPropertyChanged
)。每个自定义对象都有一个唯一的计时器。当这些计时器通过某个值(比如10秒)时,我想将单元格的前景色改为红色

我正在使用
CellValueChanged
事件,但此事件似乎从未触发,即使我可以在
DataGridView
上看到计时器正在更改。有没有其他我应该寻找的活动?下面是我的
CellValueChanged
处理程序

private void checkTimerThreshold(object sender, DataGridViewCellEventArgs e)
    {
        TimeSpan ts = new TimeSpan(0,0,10);
        if (e.ColumnIndex < 0 || e.RowIndex < 0)
            return;
        if (orderObjectMapping[dataGridView1["OrderID", e.RowIndex].Value.ToString()].getElapsedStatusTime().CompareTo(ts) > 0)
        {
            DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
            cellStyle.ForeColor = Color.Red;
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = cellStyle;
        }
    }
private void checkTimerThreshold(对象发送方,DataGridViewCellEventArgs e)
{
TimeSpan ts=新的TimeSpan(0,0,10);
如果(e.ColumnIndex<0 | | e.RowIndex<0)
返回;
如果(orderObjectMapping[dataGridView1[“OrderID”,e.RowIndex].Value.ToString()].getElapsedStatusTime().CompareTo(ts)>0)
{
DataGridViewCellStyle cellStyle=新DataGridViewCellStyle();
cellStyle.ForeColor=颜色.红色;
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style=cellStyle;
}
}

据我所知,当DataGridView的数据源以编程方式更改时,无法使其引发事件-这是出于设计

我能想到的满足您需求的最佳方法是在混合中引入BindingSource—绑定源在其数据源更改时确实会引发事件

类似的方法很有效(您显然需要根据自己的需要对其进行微调):


另一种方法是直接订阅数据,如果它是BindingList,它将使用自己的ListChanged事件传播NotifyPropertyChanged事件。在更为MVVM的场景中,可能会更干净,但在WinForms中,BindingSource可能是最好的。

您没有100%清楚地说明您要做什么。我将根据我的最佳猜测来回答,但是你能编辑一下你的问题,让它清楚地表明你想要达到的目标吗?对不起,我应该说得更清楚一些。用户不进行编辑。CSV文件不断被解析以从BindingList添加/更新/删除对象。假设我启动程序,DGV中只有一行。我将看到计时器每秒钟递增一次,当它超过10秒时,我想将文本的颜色更改为红色。刚刚用一些应该适合您的内容编辑了我的答案。很抱歉花了这么长时间,但谢谢!我使用了NotifyPropertyChanged事件,现在一切都很好!
bindingSource1.DataSource = tbData;
dataGridView1.DataSource = bindingSource1;
bindingSource1.ListChanged += new ListChangedEventHandler(bindingSource1_ListChanged); 

public void bindingSource1_ListChanged(object sender, ListChangedEventArgs e)
{
    DataGridViewCellStyle cellStyle = new DataGridViewCellStyle(); 
    cellStyle.ForeColor = Color.Red;

    dataGridView1.Rows[e.NewIndex].Cells[e.PropertyDescriptor.Name].Style = cellStyle;
}