Binding Context Question/Problem

J

Jerry

I'm having a BindingContext problem. I have a Windows Form with several
controls on it that are intended to display data from different tables in a
DataSet. There are ChildRelations defined for the DataSet so that all of the
display data relates back to an active entry. When the DataSet is filled and
I set the BindingContext position for the Form all of the data is display
correctly. The problem I'm having is when I create a new blank enty. During
the loading of the Form I create new rows in the DataSet and set the key
values to establish the parent child relationships for the new rows. The
ContextBinding position is set to this new active entry. As I then edit the
display values all seems to be fine. I can inspect the DataSet and see the
changes being recorded. Then I apply the changes (updating the data source
with specific rows of data related to the active enty, not the entire
DataSet). I can inspect the DataSet at this point and all looks good.
However as control is returned back to the Window Form all of the control
seem to disconnect from the DataSet and their displays are blank. Do I need
to adjust the BindingContext in some way after the update. The row positions
haven't change so the BindingContext position should be ok. I don't have
this problem when I modify data that preexisted in the DataSet, it is only
when I add the rows for the new entry.
Below is the code that creates the BindingContext and binds the data to some
of the controls. Am I missing something?

//Identify DataRow of interest
DataRowCollection drcEntry = EntryDataSet.Tables["entries"].Rows;
for( intEntryRow = 0; intEntryRow < drcEntry.Count; intEntryRow++ )
if( (int)drcEntry[intEntryRow]["entry_id"] == EntryId )
{
//Create and initialize BindingContext
this.BindingContext = new BindingContext();
this.BindingContext[EntryDataSet, "entries"].Position = intEntryRow;
break;
}
//Bind controls to DataSet
txtID.DataBindings.Add(new Binding("Text", EntryDataSet,
"entries.EntriesToDescriptors.identifier"));
txtDescription.DataBindings.Add(new Binding("Text", EntryDataSet,
"entries.EntriesToDescriptors.description"));
cboConfig.DataSource = ViewConfig;
cboConfig.DisplayMember = "description";
cboConfig.ValueMember = "ce_id";
cboConfig.DataBindings.Add(new Binding("Text", EntryDataSet,
"entries.EntriesToConfigs.description"));
 
J

Jerry

I've found that if I set the context position to -1 and then back to the
appropriate row in the table all is well. It would be nice to understand why
this is so. Any thoughts?

Thanks,
Jerry
 
D

Duke Sun

Do you meams that when you call AddNew to add a new entry, the textbox and
other items displayed will be cleared?

When you create a new record, all fields in the new record will be set to
default value (usually null). The cursor will be moved to the current
record and all displayed items will be cleared.

If that's not the case, could you please provide a sample to reproduce the
issue?

Best regards,

Duke Sun
Microsoft Online Partner Support
<MCSE/MCDBA/MCSD>

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 
J

Jerry

I believe the issue is that the new rows that are added prior to creating
the binding have the Added RowState. When the select rows of the dataset are
updated through the data adapter and then merged with the original dataset
the added rows become new rows and this messes with the binding. I read that
the dataset needs to be defined prior to binding to it and this probably
tweaks the binding context. So, do you think that I should delete the
original BindingContext and then create a new one? The problem I have with
that is that the row key has been updated when added to the datasource and I
don't know what it came back as.
 

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