Why is my dynamically added event handler not firing consistently - c#

Hello i basically added datagridviews dynamically to my windows form application, and added cellClick event handlers dynamically by looping through all the datagridview control, however my event doesnt fire consistently, like when i click really fast it wont clear the selection sometimes. here is my code
void DGV_CellClick(Object sender, EventArgs e)
{
DataGridView dgv = (DataGridView)sender;
dgv.ClearSelection();
}
foreach(KeyValuePair<int,datagridview>entry in DGVCollection)
{
datagridview dgv = entry.value;
dgv.CellClick+= DGV_CellClick;
}

"however my event doesnt fire consistently, like when i click really fast it wont clear the selection sometimes. here is my code"
It's possible that the CellDoubleClick event get's fired instead of the CellClick event.
You could take a look at this link

Related

C# Windows Forms CheckedChanged lags behind

I have the following problem with System.Windows.Forms (C#):
I have a CheckBox in my program and I defined an event handler for CheckedChanged. The problem is that when the user clicks the CheckBox, it may happen that it takes several seconds until the CheckBox is visibly marked as checked.
I set a breakpoint inside the CheckedChanged event and noticed that indeed, it sometimes takes several seconds until the CheckedChanged event fires. How can it be that the CheckedChanged event lags behind that much?
Unfortunately I have not been able to find information in the literature regarding the matter when exactly the CheckedChanged event is triggered. Might it be that another event is handled first before the CheckedChanged event is triggered, so I could catch this event instead and make the check-arrow appear in time?
Thank you for your help and suggestions.
The CheckedChanged event occurs when the Checked property of the CheckBox changes.
The UI will not update the checkmark inside the checkbox, until after any eventhandler for this event has been handled. If you are doing a lot of processing in the handler for the CheckedChanged event, then it will take some time before the checkmark is added/removed from the checkbox.
If you need the UI to update quickly, then consider doing the processing in a separate thread. This can be done pretty easily, using Task.
Here's a quick example:
private void MyCheckBox_CheckedChanged(object sender, EventArgs e)
{
//Don't do this:
//ThreeSecondMethod();
//Instead, do this:
Task.Run(() => ThreeSecondMethod());
}
private void ThreeSecondMethod()
{
DateTime deadline = DateTime.Now.AddSeconds(3);
while(DateTime.Now < deadline)
{
/* Do nothing */
}
MessageBox.Show("Done!");
}

Repository Combo BoxEdit EditValueChanged fires many times while typing.

I have this Repository Item comboboxEdit in a Devexpress CustomGridView.
private void gridView1_CustomRowCellEditForEditing(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
{
if (e.Column == this.gcCol1)
{
var repositoryItem = new RepositoryItemComboBox();
foreach (var title in this.ViewModelList.Titles)
{
repositoryItem.Items.Add(title.TitleName);
}
repositoryItem.EditValueChanged += this.PostEditValueChanged;
repositoryItem.Validating+=this.validating;
e.RepositoryItem = repositoryItem;
}
}
private void PostEditValueChanged(object sender, EventArgs e)
{
this.gridView1.PostEditor();
}
EditValueChanged fires many times while typing. Is there a way to fire this EditValueChanged once after the user has completely finished editing the cell.
Something along these lines http://www.devexpress.com/Support/Center/Question/Details/Q288616
Devexpress Support had some fix for this problem but didn't seem to help.
Not sure why the activeedior is closing and resetting the cursor.
I don't want to be setting the caret position in EditValueChanged.
I also tried CellvalueChanged but this would require a click in the usercontrol.
Same with repository.validating
repositoryItem.EditValueChanged += this.PostEditValueChanged;
repositoryItem.Validating+=this.validating;
Is there a way to figure out if the user is done or still editing the combox box and then fire the editvaluechanged without having to worrying out clicks outside the combo box edit
A better approach:
Handle the GridView's CellValueChanged event, rather than EditValueChanged on the editor.
In the handler, determine which column fired the event. For example,
if (e.Column.Equals(this.gvColTitle))
{
//Access the repository item:
ComboBoxEdit editor = this.gridView1.ActiveEditor as ComboBoxEdit;
//Assign your values to the editor.
}
I'm not sure why you're adding the repository item at runtime, but you may be able to just create it in the XtraGrid Designer screen, and assign it to the column there. You can still update its item list at runtime using the above.
I was able to resolve this issue by not firing the EditvalueChanged and using the Validating event.
This event fires when the editor is about to lose focus. Its unlike CellvalueChanged where if the user clicks on the form and not on the usercontrol, the change is lost.
gridView1.PostEditor(); will show the editor after populating the values. Similarly, We can change the validating event to fire on 'Enter Key' to resolve as a quick fix.

Manually trigger DataGridView event c#

I have a winforms app and a DataGridView control for which I would like to manually trigger the DataBindingComplete event so that the function below will be run.
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
DoStuff();....
}
How can I also force the manual firing of the event apart from the DataGridView automatically firing it?
Call :
dataGridView1_DataBindingComplete(dataGridView1, new DataGridViewBindingCompleteEventArgs(ListChangedType.ItemAdded));
or replace ItemAdded by anything using intellisense.

C# WinForm DataGridView Filter pause

I have a VisualStudio generated DataSet.
I connected them into a DataGridView (width connected by VisualStudio).
I'm using a filter. For example:
xyBindingSource.Filter = "yx = 'tart'";
My problem:
If I change any value of yx column (from tart to anything else), the changed row will remove before a CellEndEdit event going to run.
And in a CellEndEdit event, the DataGridViewCellEventArgs will contains the correct row and column number.
But the row what is pointed by the event args is not that, what is edited, because the selected row is removed earlier.
What can I do?
Thanks for help:
Norbi
You can handle this by using the DataGridView.CurrentCellDirtyStateChanged Event. This can raise the DataGridView.CellValueChanged Event, if you do it like this:
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
CommitEdit manually raises the DataGridView.CellValueChanged Event. You can reload your Filter Method inside this Event again. Give it a try.

Wpf DataGrid DoubleClick has weird behaviour

I am working with Wpf DataGrid. I have handled MouseDoubleClick event of DataGrid to open the record in detail in separate page. So the functionality should be like I double click on a record and it should open in separate page. Currently When I double-click on the DataGrid Header(column header) or on ScrollBar it takes double-click of the selected row(selected record). I want it to take double click of row if only double-clicked on the row. Any help please!!
Try to handle LoadingRow event in DataGrid and then the DoubleClick event in every row:
private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.MouseDoubleClick += new MouseButtonEventHandler(Row_MouseDoubleClick);
}
When resorting the DataGrid (clicking on column headers) the LoadingRow event is fired again sometimes.
I had to unsubscribe and resubscribe to the MouseDoubleClick to make this work:
private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e) {
e.Row.MouseDoubleClick -= Row_MouseDoubleClick;
e.Row.MouseDoubleClick += Row_MouseDoubleClick;
}

Categories