C#DataGridView-数据源引起的单元格更改事件
DataGridView中的单元格值因数据源中的更改而更改时是否存在事件 我已经创建了自己的自定义类,实现了INotifyPropertyChangedC#DataGridView-数据源引起的单元格更改事件,c#,datagridview,C#,Datagridview,DataGridView中的单元格值因数据源中的更改而更改时是否存在事件 我已经创建了自己的自定义类,实现了INotifyPropertyChanged public class CustomWorkbook : INotifyPropertyChanged { string filepath; string status; public event PropertyChangedEventHandler PropertyChanged; private voi
public class CustomWorkbook : INotifyPropertyChanged
{
string filepath;
string status;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
...
}
并将其绑定到我的DataGridView,如下所示
BindingList<CustomWorkbook> workbookList = new BindingList<CustomWorkbook>();
BindingSource workbookBinding = new BindingSource(workbookList , null);
dataGridViewWorkbooks.DataSource = workbookBinding;
BindingList工作簿列表=新建BindingList();
BindingSource workbookBinding=新的BindingSource(workbookList,null);
dataGridViewWorkbooks.DataSource=workbookBinding;
目前,单元格值会根据需要自动更新,但我还想添加一些处理效果和美学效果,这需要知道单元格值何时更改,以及哪个单元格(即,将更新后的单元格变为绿色,将待处理的单元格变为黄色)
我在DataGridView中尝试了CellValueChanged事件,但这似乎只适用于用户编辑。NotifyPropertyChanged事件将在值更改时激发…但它不提供对已更改单元格的任何引用 在采取蛮力方法,只向大多数
DataGridView
事件添加事件处理程序之后,我发现DataBindingComplete
事件就是我想要的。每当myCustomWorkbook
类中的属性发生更改时,就会引发此事件(我假定INotifyPropertyChanged
会逐渐进入myDataGridView
中的BindingList
,BindingSource
,最后是数据源
)
虽然此事件没有为已更改的属性提供对相应单元格的任何引用,但由于我知道包含所述单元格的列的名称,因此我最终只是在all单元格中循环
/// <summary>
/// Called whenever changes have been made to the binded list
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGridViews_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
DataGridViews_UpdateStatusColour(sender as DataGridView);
}
/// <summary>
/// Change the colour of the cells in the column whose DataPropertyName is "Status"
/// </summary>
/// <param name="grid"></param>
private void DataGridViews_UpdateStatusColour(DataGridView grid)
{
// Get the column index
int targetColumn = 0;
foreach (DataGridViewColumn col in grid.Columns)
{
if (col.DataPropertyName == "Status")
{
targetColumn = col.Index;
break;
}
}
// Loop through every row, and colour the corresponding cell
foreach (DataGridViewRow row in grid.Rows)
{
DataGridViewCell cell = row.Cells[targetColumn];
switch (cell.Value.toString())
{
case ("UPDATED"):
cell.Style.BackColor = System.Drawing.Color.Green;
cell.Style.SelectionBackColor = System.Drawing.Color.Green;
break;
case ("PENDING"):
cell.Style.BackColor = System.Drawing.Color.Orange;
cell.Style.SelectionBackColor = System.Drawing.Color.Orange;
break;
case ("MISSING"):
cell.Style.BackColor = System.Drawing.Color.LightSalmon;
cell.Style.SelectionBackColor = System.Drawing.Color.LightSalmon;
break;
case ("ERROR"):
cell.Style.BackColor = System.Drawing.Color.Red;
cell.Style.SelectionBackColor = System.Drawing.Color.Red;
break;
default:
break;
}
}
}
//
///每当对绑定列表进行更改时调用
///
///
///
私有void DataGridViews\u DataBindingComplete(对象发送方,DataGridViewBindingCompleteEventTarget e)
{
DataGridView_UpdateStatusColor(发送方为DataGridView);
}
///
///更改DataPropertyName为“状态”的列中单元格的颜色
///
///
私有void DataGridViews\u UpdateStatusColor(DataGridView网格)
{
//获取列索引
int targetColumn=0;
foreach(grid.Columns中的DataGridViewColumn列)
{
if(col.DataPropertyName==“Status”)
{
targetColumn=列索引;
打破
}
}
//循环遍历每一行,并给相应的单元格上色
foreach(grid.Rows中的DataGridViewRow行)
{
DataGridViewCell单元格=行。单元格[targetColumn];
开关(cell.Value.toString())
{
案例(“更新”):
cell.Style.BackColor=System.Drawing.Color.Green;
cell.Style.SelectionBackColor=System.Drawing.Color.Green;
打破
案件(“未决”):
cell.Style.BackColor=System.Drawing.Color.Orange;
cell.Style.SelectionBackColor=System.Drawing.Color.Orange;
打破
案件(“失踪”):
cell.Style.BackColor=System.Drawing.Color.LightSalmon;
cell.Style.SelectionBackColor=System.Drawing.Color.LightSalmon;
打破
案例(“错误”):
cell.Style.BackColor=System.Drawing.Color.Red;
cell.Style.SelectionBackColor=System.Drawing.Color.Red;
打破
违约:
打破
}
}
}
它看起来像什么:
您可能需要查看DataGridView属性SelectedCells和CurrentCell-这可能有助于缩小编辑的单元格范围。非常感谢您的回答!在我的问题()中,我一直在寻找解决方案,但没有人相信这是一个问题。我只希望它能把换过的手机还给我。。。