Prevent DataGridView RowEnter event on load
c#, winforms
Solution
You can attach the wire handler after the form is loaded, something like this:
protected override void OnShown(EventArgs e) {
base.OnShown(e);
dgStation.RowEnter += dgStation_RowEnter;
}
Make sure to remove the current RowEnter handler from the designer file.
Or just use a loading flag:
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());
}
}
Problem
When page is load than datagridview will be bind from database. I need row enter event for selecting row and based on retriving data from database. But at load time it should not happen. How can I do this ? This is my code ``` 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()); } ```