How can I add an event handler for when a Devexpress TreeList selection changes? Here's what I have that isn't working:
window.nList.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(nList_SelectedItemChanged);
private void nList_SelectedItemChanged(object sender, DevExpress.Xpf.Grid.SelectedItemChangedEventArgs e)
{
System.Diagnostics.Debug.WriteLine(nList.CurrentCellValue);
}
Are you using the multi-select mode? The SelectedItemChanged and SelectionChanged events aren't fired if the SelectionMode property is set to MultiSelectMode.None(default value).
Please use the CurrentItemChanged event instead when single-selection mode is active.
This event occurs after the focused row has been changed (e.g. row focus moves to another data row).
Just use TreeListControl.SelectionChanged event.
In XAML:
<dxg:TreeListControl x:Name="treeListControl1" SelectedItemChanged="treeListControl1_SelectedItemChanged" />
Or in c#:
treeListControl1.SelectedItemChanged += treeListControl1_SelectedItemChanged;
Event handler method:
void treeListControl1_SelectedItemChanged(object sender, SelectedItemChangedEventArgs e)
{
MessageBox.Show(((YourClass)e.NewItem).SomeValue.ToString());
}
Related
I need to update the edit value of the CheckedComboBoxEdit control immediately after the item has been checked
The MouseUp event can be used to trigger EndEditwhich will commit the change slightly faster.
I used this to solve my simular issue when working with a CheckBox within a DateGridView. As by default the event to submit changes in a DataGridView only fires when leaving the cell.
You should subscribe Popup event of CheckedComboBoxEdit, find CheckedListBoxControl and subscribe ItemCheck event. Like this:
void _orgStructEntitesCheckedComboBoxEdit_Popup(object sender, EventArgs e)
{
var popup = (IPopupControl)sender;
var control = popup.PopupWindow.Controls.OfType<PopupContainerControl>().First().Controls.OfType<CheckedListBoxControl>().First();
control.ItemCheck += control_ItemCheck;
}
void control_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
{
var checkedListBoxControl = (CheckedListBoxControl)sender;
var current = checkedListBoxControl.Items[e.Index];
}
Use e.Index to get current item changed.
More information here and here.
I'm using a WPF DataGrid with c#/xaml in Visual Studio 2013.
With SelectionMode="Extended", I'm able to multi-select rows in the grid.
I have a requirement where clicks on one of the columns of the grid are to be ignored relative to row selection.
I setup a PreviewMouseLeftButtonDown event that gets called.
Since it's a preview event, at the time of the event is processed, the selection in the grid hasn't changed yet.
I'm able to determine the row and column of the click, so I can determine a click has been made in a column that I don't want
I want to be able to abort the click event at that point so that no change is made to the current selected items in the grid. Is that possible?
In the mouse down event I tried something like:
private void GridCtrl_MouseDown(object sender, MouseButtonEventArgs e)
{
// ... Other code
e.Handled = true;
}
But, despite being marked as handled, it still continues and performs the row selection.
I also have a 'SelectionChanged' event that I see that it later gets into.
I think you actually need to handle both tunneling events - one for PreviewLeftMOuseButtonDown and another for PreviewSelectionChanged.
My advice is create a flag, let's call it:
bool _cancelSelectionChange = false;
Then, in your Mouse handler:
private void GridCtrl_MouseDown(object sender, MouseButtonEventArgs e)
{
_cancelSelectionChange = false;
// ... Other code
_cancelSelectionChange = true;
e.Handled = true;
}
Finally, in your selection change handler for the tunneling event:
private void GridCtrl_PreviewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
e.Handled = _cancelSelectionChange;
}
I'm filling a combobox with the datasource property in a c# winform app. In the other hand, I'm firing up an action with the SelectedIndexChanged of the same combo. The problem is that whenever the combo is filled with datasource the SelectedIndexChanged is called and I just want this event to be called when the user in fact does a selection.
Is there a way to avoid calling this event when filling the combo?
This is some of my code
//Filling the combo with some data
combo_cliente.DataSource = clientes;
combo_cliente.DisplayMember = "NomComp";
combo_cliente.ValueMember = "IDPersona";
private void combo_cliente_SelectedIndexChanged(object sender, EventArgs e)
{
// Here is the action to be triggered when user perfoms a selection
}
Thanks
Maybe unsubscribe and then subscribe again:
combo_cliente.SelectedIndexChanged -= combo_cliente_SelectedIndexChanged;
combo_cliente.DataSource = clientes;
combo_cliente.SelectedIndexChanged += combo_cliente_SelectedIndexChanged;
im assuming you assigned the event handler with the designer so they are bound when the control is instantiated. alternatively you could assign them in code after populating the controls.
Attach your event handler in your code behind instead of doing it on your aspx page and do it affer you have finished loading your control.
you need to add a blank record as the first of your combobox. Then in your code, you can write this;
private void combo_cliente_SelectedIndexChanged(object sender, EventArgs e)
{
if!(comboBox1.SelectedValue.ToString()== string.Empty)
{
//Here is the action to be triggered when user perfoms a selection
}
}
What is the difference between the CellClick event and the SelectionChanged event in the Windows Froms DataGridView control?
And when exactly is the selection changed event run: before the form load event or after?
The best reference for this sort of question is the MSDN DataGridView documentation.
For the CellClick event they say:
This event occurs when any part of a cell is clicked, including
borders and padding. It also occurs when the user presses and releases
the SPACE key while a button cell or check box cell has focus, and
will occur twice for these cell types if the cell is clicked while
pressing the SPACE key.
For the SelectionChanged event:
This event occurs whenever cells are selected or the selection is
canceled, whether programmatically or by user action. For example,
this event is useful when you want display the sum of the currently
selected cells.
The obvious difference is that the CellClick can fire even when the DataGridView selection does not change, for example with a right click or when clicking on the currently selected cell. Also the selection can change without a cell being clicked, for example when you change the selection programatically.
As for when exactly the selection changed event is run in relation to the form load event, when attached in the form constructor it is before (and several times at that!).
I just proved that to myself with the following code:
public Form1()
{
InitializeComponent();
MyBindingList<BackingObject> backing_objects = new MyBindingList<BackingObject>();
backing_objects.Add(new BackingObject{ PrimaryKey = 1, Name = "Fred", Hidden = "Fred 1"});
dataGridView1.DataSource = backing_objects;
this.Load += new EventHandler(Form1_Load);
dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);
}
void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Load");
}
void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
Console.WriteLine("Selection Changed");
}
The output window reads:
Selection Changed
Selection Changed
Selection Changed
Load
Note that you can make the selection changed fire after the load event by attaching it during the DataBindingComplete event handler.
dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);
}
Now in the output window you only see:
Load
And there is no selection changed output until the grid selection is changed (by for example a cell click)
I've added a handler for the CellFormatting event on a DataGridView to modify the background color based on the content of the row.
It doesn't seem to be firing even as data gets inserted into the table. I added the event handler by doubleclicking in the IDE on the CellFormatting event which seemed to create the code properly.
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// this never gets called
MessageBox.Show("Event fired");
}
What could I be doing wrong?
I think you cannot use CellFormating event for your case. It occurs when the contents of a cell need to be formatted for display.
Try CellValueChanged event instead (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx)
Or
Select other appropriate event from http://msdn.microsoft.com/en-us/library/x4dwfh7x.aspx
You could try the RowValidated event:
private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue;
}
NOTE: This event will fire when you click on rows and when you close the form.