Binding on ComboBox.SelectedValue - no Format-Event sent
I've got a problem trying to bind on SelectedValue of a ComboBox - if a value of the field in the DataRow is DBNull the ComboBox show empty. But I want it to show a 'no selection'-Item that I added to the DataTable especially for that.
I've tried to do it adding ConvertEventHandler on Parse- and Format-Event, but only that one for Parse get called.
So Ireportedit as a bug to MSDN Product Feedback Center. Now I have an answer from them, that this is a bug and it has been fixed in Visual Studio 2005. The problem is - VS2005 is now only Beta2 and I can neither wait till it will be released nor develope commercial software based on a beta.
Now a question: can somebody tell me how can I coerce that damned CurrencyManager into sending this Format-Event?
My code (the whole solution ishere) public
{
InitializeComponent();
DataRow dr =this.ds.Language.NewRow();
dr["ID"] = 0;
dr["Text"] = "no selection";
this.ds.Language.Rows.Add(dr);
dr =this.ds.Language.NewRow();
dr["ID"] = 1;
dr["Text"] = "English";
this.ds.Language.Rows.Add(dr);
dr =this.ds.Language.NewRow();
dr["ID"] = 2;
dr["Text"] = "German";
this.ds.Language.Rows.Add(dr);
DataRow drp =this.ds.Person.NewRow();
drp["ID"] = 0;
drp["Name"] = "Smith";
drp["LanguageID"] = System.DBNull.Value;
this.ds.Person.Rows.Add(drp);
this.comboLanguage.ValueMember = "ID";
this.comboLanguage.DisplayMember = "Text";
this.comboLanguage.DataSource =this.ds.Language;
Binding comboBinding =new Binding("SelectedValue",this.ds, "Person.LanguageID");
comboBinding.Parse +=new ConvertEventHandler(comboBinding_Parse);
comboBinding.Format +=new ConvertEventHandler(comboBinding_Format);
this.comboLanguage.DataBindings.Add(comboBinding);
}
//ConvertEventHandler-functions
privatevoid comboBinding_Format(object sender, ConvertEventArgs e)
{
if(e.Value.GetType() ==typeof(System.DBNull))
e.Value = 0;
elseif(e.Value.GetType() ==typeof(int))
{
if((int)e.Value == 0)
e.Value = System.DBNull.Value;
}
}
privatevoid comboBinding_Parse(object sender, ConvertEventArgs e)
{
if(e.Value.GetType() ==typeof(int))
{
if((int)e.Value == 0)
e.Value = System.DBNull.Value;
}
}

