Connecting Tech Pros Worldwide Forums | Help | Site Map

Enter null values from ComboBox

Andrus
Guest
 
Posts: n/a
#1: Dec 17 '07
I need to enter null value from combobox to business object property.

My combobox datasource does not contain ValueMember with null value.
So I tried to create combobox which stores null to bound object when text is
deleted.
However bound object contains old value in this case.

To reproduce:

1. Run code
2. Delete Walter from ComboBox
3. Click button

Observed:

s.CustId contains W

Expected:

s.CustId must be null

How to fix ?

Andrus


using System.Windows.Forms;
using System.Collections.Generic;

class testForm : Form {
Storage s = new Storage();

testForm() {
Customer c = new Customer();
c.Id = "J";
c.Name = "John";
List<Customerl = new List<Customer>();
l.Add(c);

c = new Customer();
c.Id = "W";
c.Name = "Walter";
l.Add(c);

ComboBox cm = new ComboBox();
cm.DisplayMember = "Name";
cm.ValueMember = "Id";
cm.DataSource = l;

s.CustId = "W";
cm.DataBindings.Add("SelectedValue", s, "CustId");
Controls.Add(cm);

Button b = new Button();
b.Top = 80;
b.Click += new System.EventHandler(b_Click);
Controls.Add(b);
}

void b_Click(object sender, System.EventArgs e) {

if (s.CustId == null)
MessageBox.Show("Empty combobox must store null value");
else
MessageBox.Show(s.CustId.ToString());
}

class Customer {
public string Id { get; set; }
public string Name { get; set; }
}

class Storage {
public string CustId { get; set; }
}

static void Main() {
Application.Run(new testForm());
}
}



=?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?=
Guest
 
Posts: n/a
#2: Dec 18 '07

re: Enter null values from ComboBox


Hi Andrus,

What are you ultimatly trying to achieve? You have bound the item list to
one source another to keep track of the selected value, but since
DropDownStyle is set to DropDown, anything you write in the edit box does not
update the list. It merely changes the Text property.

It is possible to add a databinding between Name and Text to force an update
of the business object, but a ComboBox really isn't suited for changing the
object, and databinding a ComboBox with edit box enabled leads to more
frustration than it is worth.

If you want to delete an item, you should not delete anything from the Items
list of the ComboBox, but rather from the underlying datasource. To have the
ComboBox update itself you also need to have a BindingSource or a BindingList
which is capable to notifying controls bound to it.

If you want to clear the selected item from storage, you could either use a
button, add a blank item, or handle the KeyDown event and set SelectedIndex
to -1 when [Del] is hit. Beware, that setting SelectedIndex to -1 will send
DBNull.Value to the business object unless you handle parse the databinding.

The easiest solution is just adding a blank item

Customer c = new Customer();
c.Id = null;
c.Name = "<Noone>";
l.Add(c);

--
Happy Coding!
Morten Wennevik [C# MVP]


"Andrus" wrote:
Quote:
I need to enter null value from combobox to business object property.
>
My combobox datasource does not contain ValueMember with null value.
So I tried to create combobox which stores null to bound object when text is
deleted.
However bound object contains old value in this case.
>
To reproduce:
>
1. Run code
2. Delete Walter from ComboBox
3. Click button
>
Observed:
>
s.CustId contains W
>
Expected:
>
s.CustId must be null
>
How to fix ?
>
Andrus
>
>
using System.Windows.Forms;
using System.Collections.Generic;
>
class testForm : Form {
Storage s = new Storage();
>
testForm() {
Customer c = new Customer();
c.Id = "J";
c.Name = "John";
List<Customerl = new List<Customer>();
l.Add(c);
>
c = new Customer();
c.Id = "W";
c.Name = "Walter";
l.Add(c);
>
ComboBox cm = new ComboBox();
cm.DisplayMember = "Name";
cm.ValueMember = "Id";
cm.DataSource = l;
>
s.CustId = "W";
cm.DataBindings.Add("SelectedValue", s, "CustId");
Controls.Add(cm);
>
Button b = new Button();
b.Top = 80;
b.Click += new System.EventHandler(b_Click);
Controls.Add(b);
}
>
void b_Click(object sender, System.EventArgs e) {
>
if (s.CustId == null)
MessageBox.Show("Empty combobox must store null value");
else
MessageBox.Show(s.CustId.ToString());
}
>
class Customer {
public string Id { get; set; }
public string Name { get; set; }
}
>
class Storage {
public string CustId { get; set; }
}
>
static void Main() {
Application.Run(new testForm());
}
}
>
>
>
Closed Thread