Binding on ComboBox.SelectedValue - no Format-Event sent

Hi all,
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 Form1()
{
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;
}
}

[5218 byte] By [Ed_G] at [2008-2-7]
# 1

This Forum is targeted at developers using VS 2005. If you have data binding questions related to prior versions of VS, please post them here: http://msdn.microsoft.com/newsgroups/managed/default.aspx?dg=microsoft.public.dotnet.framework.windowsforms.databinding&lang=en&cr=US

As for your issue, rather than binding to SelectedValue, you can bind to the SelectedIndex and use the Format/Parse events to translate SelectedIndex to SelectedValue (since you are already hooking them). The code below should work.

Joe



private void comboBinding_Format(object sender, ConvertEventArgs e)
{
if(e.Value.GetType() == typeof(System.DBNull))
e.Value = 0;
else if(e.Value.GetType() == typeof(int))
{
if((int)e.Value == 0)
e.Value = System.DBNull.Value;
}
else
{
for (int idx=0; idx<this.comboLanguage.Items.Count; idx++)
{
if ((this.comboLanguage.Items[idx] as DataRowView)["ID"] == e.Value)
{
e.Value = idx;
break;
}
}
}
}

private void comboBinding_Parse(object sender, ConvertEventArgs e)
{
if ((int)e.Value == 0)
{
e.Value = System.DBNull.Value;
}
else
{
e.Value = this.comboLanguage.SelectedValue;
}
}

JoeStegman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
Hello Joe,

I'm really sorry, that I've chosen a wrong forum again.
Great thanks for the solution you've proposed. It rescues me till VS2005 comes out. Smile
And thank you for the link - no more posts in a wrong forum. Smile

Ed_G at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...