简体   繁体   English

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

[英]How to detect cell value changed datagridview c#

There does not seem to be a definitive answer for similar questions on SOF. 对SOF的类似问题似乎没有明确的答案。

I have a DataGridView that is bound to a BindingList<T> object (which is a list of custom objects; also inherits INotifyPropertyChanged ). 我有一个绑定到BindingList<T>对象的DataGridView (它是一个自定义对象列表;也继承了INotifyPropertyChanged )。 The custom objects each have a unique timer. 每个自定义对象都有一个唯一的计时器。 When those timer's pass a certain value (say 10 seconds), I want to change the cell's forecolor to red. 当那些计时器通过一定值(比如10秒)时,我想将单元格的前景颜色更改为红色。

I am using the CellValueChanged event, but this event never seems to fire, even though I can see the timer changing on the DataGridView . 我正在使用CellValueChanged事件,但是这个事件似乎永远不会触发,即使我可以在DataGridView上看到计时器发生变化。 Is there a different event I should be looking for? 我应该寻找一个不同的事件吗? Below is my CellValueChanged handler. 下面是我的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;
        }
    }

There is no way this I know of to make the DataGridView raise an event when its DataSource is programatically changed - this is by design. 当我的DataSource以编程方式更改时,我无法让DataGridView引发事件 - 这是设计的。

The best way I can think of to meet your requirement is to introduce a BindingSource into the mix - binding sources do raise events when their DataSource changes. 我能想到满足您需求的最佳方法是将BindingSource引入混合 - 绑定源会在其DataSource更改时引发事件。

Something like this works (you will obviously need to fine tune it to your needs): 像这样的东西(你显然需要根据你的需要进行微调):

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;
}

Another option to to do this by subscribing directly to the data - if it is a BindingList it will propogate the NotifyPropertyChanged events using its own ListChanged event. 通过直接订阅数据来实现此目的的另一个选择 - 如果它是BindingList,它将使用自己的ListChanged事件传播NotifyPropertyChanged事件。 In a more MVVM scenario that would possibly be cleaner but in WinForms the BindingSource is probably best. 在一个更可能更清晰的MVVM场景中,但在WinForms中,BindingSource可能是最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM