C#学习教程:datagridviewc#如何检测单元格值变化我有一个绑定到BindingList对象的DataGridView(它是一个自定义的对象列表;也继承了INotifyPropertyChanged)。每个自定义对象都有一个唯一的计时器。当这些计时器超过某个值(比如10秒)时,我想将单元格的前景颜色更改为红色。我正在使用CellValueChanged事件,但此事件似乎从未触发,即使我可以在DataGridView上看到计时器发生变化。我应该寻找不同的活动吗?下面是我的CellValueChanged处理程序。privatevoidcheckTimerThreshold(objectsender,DataGridViewCellEventArgse){TimeSpants=newTimeSpan(0,0,10);if(e.ColumnIndex<0||e.RowIndex0){DataGridViewCellStylecellStyle=newDataGridViewCellStyle();=颜色.红色;dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style=cellStyle;我不知道如何让DataGridView在其DataSource以编程方式更改时引发事件-这是设计使然。我能想到的满足您需求的最佳方法是将BindingSource引入组合中——当DataSource发生变化时,BindingSource会引发事件。像这样的东西(你显然需要根据你的需要对其进行微调):bindingSource1.DataSource=tbData;dataGridView1.DataSource=bindingSource1;bindingSource1.ListChanged+=newListChangedEventHandler(bindingSource1_ListChanged);publicvoidbindingSource1_ListChanged(objectsenderE,ListArchanged){DataGridViewCellStylecellStyle=newDataGridViewCellStyle();cellStyle.ForeColor=Color.Red;dataGridView1.Rows[e.NewIndex].Cells[e.PropertyDescriptor.Name].Style=cellStyle;另一个通过直接订阅数据Select来做到这一点——如果它是一个BindingList,它将使用它自己的ListChanged事件传播NotifyPropertyChanged事件。在更可能更清洁的MVVM场景中,但在WinForms中,BindingSource可能是最好的。以上就是C#学习教程:如何检测单元格值和改变datagridviewc#分享的全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
