DataGridView SelectionChanged event
-
Hi, A quote from MSDN about this event: "This event occurs whenever cells are selected or the selection is canceled, whether programmatically or by user action". My question: How can I get the exact same functionality, but only as a result of user action (I do not wish this even to occur when the selection has programmatically changed). Thanks, Eyal.
-
Hi, A quote from MSDN about this event: "This event occurs whenever cells are selected or the selection is canceled, whether programmatically or by user action". My question: How can I get the exact same functionality, but only as a result of user action (I do not wish this even to occur when the selection has programmatically changed). Thanks, Eyal.
What I can think of is to have a flag (boolean variable) in your form. Now, when you are changing selected cell through code, set this to false and make it true again in the end of event. Something like this:
bool isChangedByUser = true; // class level variable
.
.
.
isChangedByUser = false;
// Some code block that changes selected cell
.
.
.
void dgv_SelectionChanged(object sender, EventArgs e)
{
if(isChangedByUser)
{
// Do something
}
isChangedByUser = true;
} -
What I can think of is to have a flag (boolean variable) in your form. Now, when you are changing selected cell through code, set this to false and make it true again in the end of event. Something like this:
bool isChangedByUser = true; // class level variable
.
.
.
isChangedByUser = false;
// Some code block that changes selected cell
.
.
.
void dgv_SelectionChanged(object sender, EventArgs e)
{
if(isChangedByUser)
{
// Do something
}
isChangedByUser = true;
}Thank for the reply. The problem is not that I'm changing the selection - in this case the problem is easy to solve. The problem is that the event is fired when the form is initially opened and rows are automatically added. Then, when the first row is (automatically) selected, the event is fired. Eyal.
-
Thank for the reply. The problem is not that I'm changing the selection - in this case the problem is easy to solve. The problem is that the event is fired when the form is initially opened and rows are automatically added. Then, when the first row is (automatically) selected, the event is fired. Eyal.