"<default>" selection in ComboBox DataSource

G

Guest

I have a combobox whose datasource is a lookup table populated from the
database. However, the record whose field is bound to the SelectedValue of
the combo uses DBNull to indicate no selection, in which case the combobox
should display "<default>". I convert the DBNull to zero in the source
record, and I would like this to cause the default to display in the combo.

I don't have the option of adding a value to the lookup table in the db, so
I'm inserting a row with the <default> token upon loading the lookup table,
with ID=0. I do AcceptChanges on the table after the insert.

If I bind the combobox's DataSource to this altered table, binding is broken
on the form (all bound controls are blank). If instead, I create a *new*
table, add the default token, add the rows from the database table to the new
lookup table, and then bind the combo's DataSource to the new table,
everything works fine.

Is there some trick to binding the combo's datasource to the original table
loaded from the db, after the <default> row is inserted, rather than having
to create a copy? I would have thought that the AcceptChanges() would be
enough to make the two cases indistinguishable.
 
G

Guest

Hmmm ... this is an interesting post. That should not break all your other
bindings. Is this DataTable a standalone lookup table or is it a DataTable in
the DataSet that the rest of your form is bound to?

You might want to post a little bit of the relevant code (mainly, your
DataBinding code).

~~Bonnie
 
G

Guest

Bonnie Berent said:
Hmmm ... this is an interesting post. That should not break all your other
bindings. Is this DataTable a standalone lookup table or is it a DataTable in
the DataSet that the rest of your form is bound to?

You might want to post a little bit of the relevant code (mainly, your
DataBinding code).

~~Bonnie

Thanks for the response, Bonnie. The table that is the datasource for that
combo is in a different dataset than the one to which the SelectedValue is
bound.
This is the case for all the comboboxes on the form.

The code snips below show the original approach, which causes all the bound
controls on the form to be empty:

------------------------------------------------------------
// (code from glob class Couriers property get)
// populate dataset
cCustomer ds = new cCustomer();
SqlCommand cmd = new SqlCommand(@"spCustomerListGet", glob.Connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@"@sCustomerType",SqlDbType.Char, 1).Value = 'C';
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds, ds.dataCustomerList.TableName);

// get ref to table in dataset
cCustomer.dataCustomerListDataTable tbl = ds.dataCustomerList;
// provide a row for the default:
cCustomer.dataCustomerListRow row = tbl.NewdataCustomerListRow();
row.CustomerID = 0;
row.Name = row.NameAbbr = @" <default>";
row.CustomerType = cCustomer.CustomerType.Courier;
tbl.Rows.InsertAt(row,0);
tbl.AcceptChanges();
return tbl;

// (code from form constructor)
cCustomer.dataCustomerListDataTable tbl = glob.Couriers;
cboCourier.DataSource = tbl;
cboCourier.ValueMember = @"CustomerID";
cboCourier.DisplayMember = @"NameAbbr";
cboCourier.DataBindings.Add(new
System.Windows.Forms.Binding("SelectedValue", dsService,
"dataService.DefaultCourier"));

-----------------------------------------------------------------------------------

When I created a separate copy of the Couriers table, everything worked fine:
-----------------------------------------------------------------------------------

// (fill dataset ds as before)
// new table not in dataset
tbl = new cCustomer.dataCustomerListDataTable();

// provide a row for the default:
cCustomer.dataCustomerListRow row;
row = tbl.NewdataCustomerListRow();
row.CustomerID = 0;
row.Name = row.NameAbbr = @" <default>";
row.CustomerType = cCustomer.CustomerType.Courier;
tbl.Rows.Add(row);

// now add the actual couriers
foreach (cCustomer.dataCustomerListRow r in ds.dataCustomerList)
{
row = tbl.NewdataCustomerListRow();
row.CustomerID = r.CustomerID;
row.Name = row.NameAbbr = r.NameAbbr;
row.CustomerType = cCustomer.CustomerType.Courier;
tbl.Rows.Add(row);
}
tbl.AcceptChanges();
return tbl;
 
G

Guest

Weird stuff. Only thing I can think to ask is: Does your cCustomer DataSet
class have any static tables?

I think you *might* be able to get around this by trying this:

Leave your glob Couriers get as you originally had it (not creating a
separate copy of the table). But use it this way instead:

// (code from form constructor)
DataTable tbl = glob.Couriers;
cboCourier.DataSource = tbl;
..... etc.

Since you're simply using this table as a source of a Combo, you really
don't need to have it typed as a dataCustomerListDataTable, just a plain old
DataTable will do.

Try it and lemme know if that works.

~~Bonnie
 
G

Guest

Bonnie: Unfortunately, that made no difference. I also tried creating a
separate DataView and using that as the datasource. No difference. I'll
just leave the copy operation in the code and move on -- it's only performed
once, and I've gotta meet deadlines. Bound controls have always been pretty
spooky! ;-)
 
G

Guest

Weird ... if I get some free time, I'll try to play around with this and see
if I can duplicate your problem.

~~Bonnie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top