S
Samuel
Hello:
I first put the code and then I'll comment it:
.... somewhere in Form_Load ...
Binding binding = new Binding("Value", myTableDataView, "StartupDate");
binding.Format = new ConvertEventHandler(StartupDateBinding_Format);
StartupDateDateTimePicker.DataBindings.Add(binding);
....
private void StartupDateBinding_Format(object sender, ConvertEventArgs e)
{
if (e.Value == null || e.Value == DBNull.Value)
e.Value = new DateTime(2003, 1, 1);
}
....
The problem comes when I do:
((CurrencyManager)BindingContext[myTableDataView]).AddNew();
The StartupDateBinding_Format method is not called, and an exception is
thrown which says "DataBinding could not find a row in the list that is
suitable for all bindings".
It couldn't find a row suitable for all bindings, because my
StartupDateBinding_Format method has not been called, and it cannot set the
DateTime Value property of a DateTimePicker object to null or DBNull.
And also I'm having trouble creating my own editor control:
public class DateTimeEditBox : System.Windows.Forms.TextBox
{
private DateTime _value;
public DateTimeEditBox()
{
DataBindings.CollectionChanged += new
CollectionChangeEventHandler(DataBindings_CollectionChanged);
}
public DateTime Value
{
get
{
return _value;
}
set
{
if (value == _value) return;
_value = value;
...
if (ValueChanged != null) ValueChanged(this, EventArgs.Empty);
}
}
public event EventHandler ValueChanged;
private void DataBindings_CollectionChanged(object sender,
CollectionChangeEventArgs e)
{
if (e.Action == CollectionChangeAction.Add)
((Binding)e.Element).Format += new
ConvertEventHandler(Binding_Format);
}
private void Binding_Format(object sender, ConvertEventArgs e)
{
if (e.Value == null || e.Value == DBNull.Value)
e.Value = new DateTime(2003, 1, 1);
}
}
Well this control has the Format event handler embedded so I don't need to
do a "binding.Format = new ConvertEventHandler(StartupDateBinding_Format);",
but it's the same.
But surprisingly this time the Binding_Format method is called and it works
fine. But... when I change the Value property, the value in the bounded row
doesn't change, even when I'm raising ValueChanged. And I checked that when
I raise ValueChanged there is one method listening (ValueChanged is not
null), which is the handler method generated by the Binding.
No exception is thrown, nothing. And it should work because the column which
I bound is a DateTime DataColumn.
Why all this strange behavior? Maybe the bindings have problems with
value/reference types? Or am I doing something wrong (I don't think so,
but...)?
By the way, I'm using Visual Studio .NET 2003 / .NET Framework 1.1.
Thank you in advance.
Samuel, from Spain
I first put the code and then I'll comment it:
.... somewhere in Form_Load ...
Binding binding = new Binding("Value", myTableDataView, "StartupDate");
binding.Format = new ConvertEventHandler(StartupDateBinding_Format);
StartupDateDateTimePicker.DataBindings.Add(binding);
....
private void StartupDateBinding_Format(object sender, ConvertEventArgs e)
{
if (e.Value == null || e.Value == DBNull.Value)
e.Value = new DateTime(2003, 1, 1);
}
....
The problem comes when I do:
((CurrencyManager)BindingContext[myTableDataView]).AddNew();
The StartupDateBinding_Format method is not called, and an exception is
thrown which says "DataBinding could not find a row in the list that is
suitable for all bindings".
It couldn't find a row suitable for all bindings, because my
StartupDateBinding_Format method has not been called, and it cannot set the
DateTime Value property of a DateTimePicker object to null or DBNull.
And also I'm having trouble creating my own editor control:
public class DateTimeEditBox : System.Windows.Forms.TextBox
{
private DateTime _value;
public DateTimeEditBox()
{
DataBindings.CollectionChanged += new
CollectionChangeEventHandler(DataBindings_CollectionChanged);
}
public DateTime Value
{
get
{
return _value;
}
set
{
if (value == _value) return;
_value = value;
...
if (ValueChanged != null) ValueChanged(this, EventArgs.Empty);
}
}
public event EventHandler ValueChanged;
private void DataBindings_CollectionChanged(object sender,
CollectionChangeEventArgs e)
{
if (e.Action == CollectionChangeAction.Add)
((Binding)e.Element).Format += new
ConvertEventHandler(Binding_Format);
}
private void Binding_Format(object sender, ConvertEventArgs e)
{
if (e.Value == null || e.Value == DBNull.Value)
e.Value = new DateTime(2003, 1, 1);
}
}
Well this control has the Format event handler embedded so I don't need to
do a "binding.Format = new ConvertEventHandler(StartupDateBinding_Format);",
but it's the same.
But surprisingly this time the Binding_Format method is called and it works
fine. But... when I change the Value property, the value in the bounded row
doesn't change, even when I'm raising ValueChanged. And I checked that when
I raise ValueChanged there is one method listening (ValueChanged is not
null), which is the handler method generated by the Binding.
No exception is thrown, nothing. And it should work because the column which
I bound is a DateTime DataColumn.
Why all this strange behavior? Maybe the bindings have problems with
value/reference types? Or am I doing something wrong (I don't think so,
but...)?
By the way, I'm using Visual Studio .NET 2003 / .NET Framework 1.1.
Thank you in advance.
Samuel, from Spain