DataGridView SelectionChanged事件总是在数据源更改时自动触发,这点很讨厌。
可以在DataBindingComplete时,再添加事件 dv8.SelectionChanged += new System.EventHandler(this.dv8_SelectionChanged);
或用CellClick和KeyUp事件和一个函数替代SelectionChanged事件
private void dvpt_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0) return;//如果不是单击列表头
MySelectionChanged();
}
private void dvpt_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Down || e.KeyData == Keys.Up)
{
MySelectionChanged();
}
}
public void MySelectionChanged()
{
if (dvpt.SelectedRows.Count == 0) return;
....................................................................................
}