DataBinding with controls on a tabpage

G

Guest

I have a problem with controls (several combobox and textbox controls) placed on the TabPages of a TabControl. The controls on the first TabPage display the data from the dataset, but a null dataset is returned from GetChanges when attempting to update. On the other tabpages, no data is displayed in the controls from the dataset, although the dataset contains the right information. All controls were bound (at design time at least) to one table in the dataset

From the reading I have done so far, it seems that container controls such as TabPage and Panel will create their own BindingContext by default. I have seen posts that suggest the BindingContext iof each TabPage should be set to the BindingContext of the Form, but others also suggesting that the BindingContext of the TabPage may change as you switch tabs, but no real explanation of how to overcome this. I have tried setting BindingContext of the TabPages but no success

I would have expected some doc on MSDN describing this, as it seems quite complex (IMHO) and not too out of the ordinary. Is there any example code out there of how to do databinding on tabpages? I assume the same applies to panels but can't find any help there either

Thanks
Scot
Member of the Microsoft Empower Program (ie newbie to C#)
 
Y

Ying-Shen Yu[MSFT]

Hi Scott,

Thanks for posting in the commuinty!

From my understanding now, you have two issues when using databinding on a
TabControl.
1. After modifing the data in the first page, GetChanges is still return
null when you attempting to update.
2. no data is populated into other tabpages, although the dataset is not
empty.
If my understandiing is now correct, please let me know.

Before dealing with these issues, I'd like to confirm the version of VS.NET
you are using now. It's VS.NET 2003(.NET Framework v1.1) right?

For the first issue, I'd like to know if your have tried changing the
navigating to next/previous row before calling the GetChanges method? If
the problem could be resolved by navigating to the next row, I suggest you
call
BindingContext[datasource, datamember].EndCurrentEdit() method before
calling GetChanges(), since it will force ending the "edit mode"on the
current row. You may get the datasouce and datamember from e.g.
textBox1.DataBindings["Text"].DataSource;
textBox1.DataBindings["Text"].BindingMemberInfo.BindingField;


For the second issue, for now I can't think of what might cause this
problem, I tried putting a datagrid on the second tab page and bound the
the dataset, datatable at design-time, and it works fine in my test
program. So could you let me know me some more information on this issue,
such as how did you bind the controls with the underlying datasources? When
did you set the data binding ,in design time or in run time? Also , it
would be helpful to post some code snippet related to this issue. I'm happy
to take a look at it to see if I could figure out something.

Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
G

Guest

Thanks for the reply. I'll respond to your questions below

You summarised the problem correctly
Yes I'm using VS.NET 2003(.NET Framework v1.1)

The datatable only has one row so its a bit tricky to navigate :). The form allows users to update options, and all these options are pulled from a single row in the database. I am calling EndCurrentEdit() but I will check that I have the specified the correct datasource and datamember. To be honest I only mentioned this because I wondered if the problems were related. If I can get data showing up on the other tabpages I think I'll be able to get the changes working on my own. I will test with additional rows and play around with EndCurrentEdit() and let you know

Each tabpage has several comboboxes on it. Each combobox binds its Text property to one column of the one table that is in the dataset. Binding is done at design time and is done the same way for each combobox regardless of which tabpage it is on. The comboboxes all have DataSource, DisplayMember and ValueMember properties set to a different dataset that holds the list of valid options. This dataset has one table for each of the comboboxes. This binds correctly and I can select values from the lists, but they do not see to bind back to the first dataset

I don't have the code with me here, but will try to send some snippets in the next few hours
Let me know if you have further questions and I'll try to reply at the same time

Scott


----- \"Ying-Shen Yu[MSFT]\" wrote: ----

Hi Scott

Thanks for posting in the commuinty

From my understanding now, you have two issues when using databinding on a
TabControl.
1. After modifing the data in the first page, GetChanges is still return
null when you attempting to update
2. no data is populated into other tabpages, although the dataset is not
empty
If my understandiing is now correct, please let me know

Before dealing with these issues, I'd like to confirm the version of VS.NET
you are using now. It's VS.NET 2003(.NET Framework v1.1) right

For the first issue, I'd like to know if your have tried changing the
navigating to next/previous row before calling the GetChanges method? If
the problem could be resolved by navigating to the next row, I suggest you
call
BindingContext[datasource, datamember].EndCurrentEdit() method before
calling GetChanges(), since it will force ending the "edit mode"on the
current row. You may get the datasouce and datamember from e.g
textBox1.DataBindings["Text"].DataSource
textBox1.DataBindings["Text"].BindingMemberInfo.BindingField


For the second issue, for now I can't think of what might cause this
problem, I tried putting a datagrid on the second tab page and bound the
the dataset, datatable at design-time, and it works fine in my test
program. So could you let me know me some more information on this issue,
such as how did you bind the controls with the underlying datasources? When
did you set the data binding ,in design time or in run time? Also , it
would be helpful to post some code snippet related to this issue. I'm happy
to take a look at it to see if I could figure out something

Thanks

Best regards,

Ying-Shen Yu [MSFT
Microsoft community Suppor
Get Secure! - www.microsoft.com/securit

This posting is provided "AS IS" with no warranties and confers no rights
This mail should not be replied directly, please remove the word "online"
before sending mail
 
G

Guest

It looks like it was my own problem :-( but I am interested to know what actually happened and why the problem was so hard to track down

The comboboxes are actually my own class derived from the standard ComboBox. All I am doing differently is to set a readonly textbox with the SelectedValue from the combobox whenever a selection is made. That way, the user can see the longer description even after a selection is made. The textbox control is specified in a property ValueControl. It looks like I had a problem where sometimes the SelectedValue is null and I try to set the textbox to that value. See event hander below

private void cmb_SelectedIndexChanged(object sender, System.EventArgs e

ColumnComboBox cmb = (ColumnComboBox)sender

// if (cmb.SelectedValue != null
cmb.ValueControl.Text = cmb.SelectedValue.ToString()


Originally I didn't have the commented line included. Now that I do, everything works perfectly. So, what was happening? I assume that calling ToString() on a null object will throw an exception, but I never saw it. Since the event handler is called from within Framework code I can only guess that the exception is caught within that code. Could that really confuse the data binding to the extent that I saw? I realise this is my problem, but wonder whether the Framework could handle this better so it was easier to track down

Thanks for your assistance and pointing me in the right direction
Scott
 
Y

Ying-Shen Yu[MSFT]

Hi Scott,

I'm not sure if the problem is fixed or not, it looks to me that where
removing the if line or not made no big difference. Also I make a test to
do with the similiar code but with a standard ComboBox class, when I set
selectedIndex = -1, I did get the NullReferenceException, so I don't think
.NET code "eat" this exception.
I'd like to know did you override the OnSelectedIndexChanged method and
handle some exceptions there?

Also, back to your initial requirement, I think we can make thing a bit
easier, you may just bind that TextBox.Text property to the same path as
comboBox.DisplayMember so you needn't write code to set the text manually,
the databinding mechanism will help you sync these two controls seamlessly.

If you have further questions on this issue, please feel free to reply this
thread.
Thanks!


Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
G

Guest

Thanks for your assistance. Yes the problem as I originally described it has been solved. I think it turns out it has to do with my limited understanding of databinding with comboboxes. I have overridden OnDrawItem to display both the DisplayMember and ValueMember in a 2 column drop down list. DisplayMember and ValueMember are columns in a lookup table in separate DataSet - not the dataset that contains selected values for each record.

I think my problem is in binding to the main dataset, not the lookup table dataset. I have played around with binding to the Text property and also the SelectedValue property. Can you point me to any discussions on the relative merits of each? For example, when I bind to SelectedValue and add a new row, the SelectedValue of the combobox is the ValueMember of the first row in the lookup table, rather than a default value of null, as I would expect. Worse, still if you try to update back to the original database with the default value from the first row of the lookup table displayed, you get an exception indicating the column in the datatable cannot be null - thrown by EndCurrentEdit(). From what I am seeing, SelectedValue is set on AddNew(), but not updating the dataset on EndCurrentEdit(). Am I missing something here

Thanks again
Scott
 

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