[英]Prevent DataGridView RowEnter event on load
當頁面加載時,datagridview將從數據庫綁定。
我需要行輸入事件來選擇行並基於從數據庫中檢索數據。
但是在加載時它不應該發生。 我怎樣才能做到這一點 ?
這是我的代碼
private void dgStation_RowEnter(object sender, DataGridViewCellEventArgs e)
{
dgStation.Rows[e.RowIndex].Selected = true;
int id = Convert.ToInt32(dgStation.Rows[e.RowIndex].Cells[1].Value.ToString());
}
您可以在加載表單后附加有線處理程序,如下所示:
protected override void OnShown(EventArgs e) {
base.OnShown(e);
dgStation.RowEnter += dgStation_RowEnter;
}
確保從設計器文件中刪除當前的RowEnter處理程序。
或者只使用加載標志:
private bool loading = true;
protected override void OnShown(EventArgs e) {
base.OnShown(e);
loading = false;
}
private void dgStation_RowEnter(object sender, DataGridViewCellEventArgs e) {
if (!loading) {
dgStation.Rows[e.RowIndex].Selected = true;
int id = Convert.ToInt32(dgStation.Rows[e.RowIndex].Cells[1].Value.ToString());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.