Unselected Combo-Value stored...

  • Thread starter Thread starter Stropher
  • Start date Start date
S

Stropher

Hi!
I've just written a form. In this form, I extract some data from the db
for a given combobox when loading the form. The problem is this, when I
fail to select an item from this combobox and store my changes, I still
observe that the first combobox-item is stored in my new table (that
means, it is chosen automatically). Is there any way I can avoid this?
I have the following in my load_method:

private void Form1_Load(object sender, EventArgs e)
{
commandTxt = "select no, name1 from ...";
tblDeliver = SelectStatementsData(commandTxt);//gets the rows
if (tblDeliver != null)
{
this.comboBoxDeliver.DataSource = tblDeliver;

//names are displayed in combo, but works with no (id)
this.comboBoxDeliver.ValueMember = "no";
this.comboBoxDeliver.DisplayMember = "name1";
}
else
MessageBox.Show(ExceptionMsg);

}

Tried to manipulate it in SelectedIndexChanged, but to no avail:
private void comboBoxDeliver_SelectedIndexChanged(object sender,
EventArgs e)
{
if (comboBoxDeliver.SelectedItem.ToString() != null)
{
comboBoxDeliver.SelectedValue.ToString();
}
else
MessageBox.Show("Deliverer does not exist");

------------------------------------------
How can I manipulate it so that nothing could appear in the first line
of combobox by loading? or/and
How can I make sure that if I select nothing from the combo, nothing
will be stored in the db on saving?

Thanks in advance,
Stropher
 
Hi Stropher,

There will always be a value selected your forms ComboBox because it
does not support all the modes that a WinForms ComboBox supports,
namely it only works in DropDownList mode.

The usual way of dealing with this problem is to put a placeholder
value in your ComboBox like <Please Select a Thing>. You can then
ignore this value or use it to trigger a validation event by marking it
as the "Default" value in a validator and disallowing it to be
submitted.

Hope that helps,
Jan
 
Back
Top