我有一个WinForms应用程序,我使用DataGridView
来显示一些快速变化的数据。我使用间隔为50-100毫秒的System.Windows.Forms.Timer()
来限制DataSource
的更新频率,从而限制用户界面的更新频率。
我希望能够删除网格中的行,因此我添加了DataGridViewButtonColumn
,我通过CellContentClick
事件处理。问题是,由于相当“高”的更新频率,此事件不会持续激发。有时它会发射,有时却不会发射。
有没有办法解决这个问题,我保持网格中的按钮,仍然使用快速的UI更新率(最大约100毫秒)?
这是我的代码的精简版本:
private IBindingList _bindingList = new BindingList<MyData>();
private BindingSource _bindingSource = new BindingSource(_bindingList, null);
private void Form1_Load(object sender, EventArgs e)
{
dataGridView.DataSource = _bindingSource;
var uiTimer = new System.Windows.Forms.Timer();
uiTimer.Interval = 100;
uiTimer.Tick += uiTimer_Tick;
uiTimer.Start();
}
private void uiTimer_Tick(object sender, EventArgs e)
{
// Get the underlying list of the binding source
var list = ((IList<MyData>)_bindingSource.List);
// Manipulate affected items in the bindinglist here...
// Update DataGridView
_bindingSource.ResetBindings(false);
}
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
e.RowIndex >= 0)
{
MessageBox.Show("Button clicked!");
// Do remove stuff here...
}
}
答案 0 :(得分:0)
一些想法......
使用SuspendBinding / ResumeBinding组合可能更有效率
_bindingSource.SuspendBinding();
// affected items in the bindinglist here...
_bindingSource.ResumeBinding();
还要确保_bindingList不会在其他位置修改,而是在计时器事件
中修改