Description:
I use the FocusedRowChanged event of the XtraGrid. The event handler contains code which takes some time to be executed. When a user scrolls the grid by holding down the up or down arrow keys, the grid scrolls slowly. I would like to prevent my code from being processed while a user is scrolling the grid. Is this possible?
Answer:
It is possible to implement this behavior. The code, which is currently executed in your FocusedRowChanged event handler should be executed by a timer. The timer should stop itself to execute the code only once.
The FocusedRowChanged event handler should start the timer. If the timer is already started (in cases when the FocusedRowChanged event is frequently generated because of continuous scrolling), the timer should be reset - stopped and restarted.
C#private void gridView1_FocusedRowChanged(object sender,
DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) {
if(checkEdit1.Checked) {
if(timer1.Enabled)
timer1.Enabled = false; // reset timer
timer1.Enabled = true; // start timer
}
else
ProcessFocusedRowChanged();
}
private void timer1_Tick(object sender, System.EventArgs e) {
timer1.Enabled = false; // self-stop timer
ProcessFocusedRowChanged();
}
// some time consuming process
public void ProcessFocusedRowChanged() {
... // your code is here
}
what if ProcessFocusedRowChanged() method needs sender object and FocusedRowChangedEventArgs ?In that case how this will work
Hi,
In this case, create a wrapper class that will store these objects. You can use the Timer's Tag property to get access to an instance of this wrapper class. As a result, you will be able to obtain event arguments and the sender object. Check the following code:
private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { if(checkEdit1.Checked) { timer.Tag = new ParametersWrapper(sender, e); if(timer1.Enabled) timer1.Enabled = false; // reset timer timer1.Enabled = true; // start timer } else ProcessFocusedRowChanged(ParametersWrapper); } private void timer1_Tick(object sender, System.EventArgs e) { timer1.Enabled = false; // self-stop timer ProcessFocusedRowChanged(timer.Tag as ParametersWrapper); } // some time consuming process public void ProcessFocusedRowChanged(ParametersWrapper args) { ... // your code is here } public class ParametersWrapper { public FocusedRowChangedEventArgs EventArgs { get; private set; } public object Sender { get; private set; } public ParametersWrapper(object sender, FocusedRowChangedEventArgs e) { this.Sender = sender; this.EventArgs = e; } }
I hope you find this information useful. Please feel free to contact us in case of further difficulties.