Replies
Each row has a property named "Selected".
Active row is the row that has the input focus, hence there is only one active row. Selected row is a row that was selected by the user or by code and there could be more than one.
If you want to get all selected rows use grid.Selected.Rows
If you want to select all rows, don't go over them and set Selected = true because this can be very slow, but use grid.Selected.Rows.AddRange(grid.Rows)
ValueChanged is fired only when the user changes the value, not when you bind and the binding changes the value.
We already solved the problem like that:
public
partial class MyComboEditor : UltraComboEditor
{
/// <summary>
/// Occurs when the value is about to be changed
/// </summary>public event EventHandler<CancelNewEventArgs> ValueChanging;
/// <summary>
/// Holds the previous value in order to allow rollback
/// </summary>protected object m_OldValue;
private bool m_SkipValueChanging;public MyComboEditor()
{
InitializeComponent();
m_OldValue = null;
}
/// <summary>
/// Rollbacks the value changing if ValueChanging was cancelled
/// </summary>
/// <param name="args"></param>protected override void OnValueChanged(EventArgs args)
{
if (!m_SkipValueChanging)
{
CancelNewEventArgs e = new CancelNewEventArgs();
OnValueChanging(e);
if (e.Cancelled)
{
m_SkipValueChanging = true;
if (m_OldValue == null)this.SelectedItem = null;
else
Value = m_OldValue;
m_SkipValueChanging = false;
}
}
base.OnValueChanged(args);
}
protected virtual void OnValueChanging(CancelNewEventArgs args)
{
if (ValueChanging != null)
{
ValueChanging(this, args);
}
}
public override object Value
{
get
{
return base.Value;
}
set
{
base.Value = value;
m_OldValue = Value;
}
}
}
That's make sense, since you can't select text unless you're in edit mode. You can get into edit mode grid.PerformAction(EnterEditMode).
My code still has a problem. If the control is bound, the OldValue should get the value of "Value", but ValueChanged is not fired as a result of binding change, so you always get a null value if you cancel the change. I'm using the combo editor inside a UserControl so I have a property that explicitly sets the Value property, but whoever want to use this need to fix it.
I can cache the old value and make my own ValueChanging event like that: (sample attached)
public event EventHandler<CancelNewEventArgs> ValueChanging;
protected object oldValue;
private bool skipValueChanging;
protected override void OnValueChanged(EventArgs args)
{
if (!skipValueChanging)
{
CancelNewEventArgs e = new CancelNewEventArgs();
OnValueChanging(e);
if (e.Cancelled)
{
skipValueChanging = true;
if (oldValue == null)
this.SelectedItem = null;
else
Value = oldValue;
skipValueChanging = false;
}
}
oldValue = Value;
base.OnValueChanged(args);
}
protected virtual void OnValueChanging(CancelNewEventArgs args)
{
if (ValueChanging != null)
{
ValueChanging(this, args);
}
}