Wpf DataGrid DoubleClick has weird behaviour - c#

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;
}

Related

C# - ComboBox - How to capture a MouseClick on a item in the list?

I read many answers, but in my case, I don't know why I can't capture the mouse click on the list items, it works only when I click on the object (ComboBox).
internal System.Windows.Forms.ComboBox MyComboBox;
this.MyComboBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyComboBox_MouseDown);
...
private void MyComboBox_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("MouseDown...");
}
When I click on the ComboBox itself or in the arrow I get the click event, but when I click on any items in the list of this ComboBox the click event never fires.
From other answers, they seem to use SelectedIndexChanged() event for Mouse Clicks... but this event also fires when it changes programmatically.
How I can detect a MouseDown on a ComboBox item?

Why does the ButtonClick event on DropDown control not fired in VSTO Ribbon?

For example, I add a DropDown Control and its ButtonClick event handler method:
private void dropDown1_ButtonClick(object sender, RibbonControlEventArgs e)
{
MessageBox.Show("I am clicked.");
}
but, when I click DropDown control, it doesn.t show the message, which mean the event not fired. How to explain this?
The event is fired when a button is clicked on the list. So, you need to fill the list of Buttons like shown on the screenshot:

Why is my dynamically added event handler not firing consistently

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

WinForms - Disable default mouse hover over item behaviour?

I have a ComboBox with items in it. I also have a SelectedIndexChange event.
When I open the ComboBox and hover over an item, the SelectedIndex property seems to change to that item. I'd only like it to change when I click on the item. Is it possible to disable that behavior?
I have a timer that refreshes an Image based on SelectedIndex of ComboBox, but still, even if I highlight an item but don't select, why does the Image changes while it should not change and it only should change when I select an item.
The Problem
When the the mouse moves on items of ComboBox, the SelectedIndex changes but SelectedIndexChanged event doesn't fire, so in your timer Tick event, you will see the change while SelectedIndexChanged doesn't fire.
Scenario of reproducing the problem
To simply reproduce the problem, put a Timer on a form and enable it, then handle its Tick event. Also add a ComboBox and add some items to it and handle its SelectedIndexChanged event. When you open the dropdown and move mouse over items, you will see the Text of form changes to the index of item that is under the cursor, while SelectedIndexChanged doesn't fire and no MessageBox will show.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(this.comboBox1.SelectedIndex.ToString());
}
private void timer1_Tick(object sender, EventArgs e)
{
this.Text = this.comboBox1.SelectedIndex.ToString();
}
The Solution for your case
You can simply check if the dropdown is not open using DroppedDown property of ComboBox, then do the job:
private void timer1_Tick(object sender, EventArgs e)
{
if(!this.comboBox1.DroppedDown)
this.Text = this.comboBox1.SelectedIndex.ToString();
}

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.

Categories