Databinding to Textboxes

G

Guest

I have a windows forms that shows only one record at a time.

The user selects the record he want via a combo box. Everything works fine
for the first selection, however the textboxes do not automatically update
their values when the combo box changes values.

The textboxes are bound to a dataset. Here is and an example of what the
databinding lookes like in designer:

Text: DsEventPerson1 - tblSubject.FirstName

When the user selects a new row in the combo box, the DsEventPerson1 is
cleared and refilled. What must I do to get the textboxes to show the
correct values for each record selected?
 
G

Guest

Well no help on this one?

The approach I took was to drop binding, clear and refill the dataset, and
re-add the bindings.

wr
 
G

Guest

What code do you have in your ComboBox's .SelectedIndexChanged Event Handler?
You need to move the BindingContext Position in order for the correct row to
show up in the bound TextBoxes.

~~Bonnie
 
G

Guest

Since each event can have anywhere from 30 to 800 grads in them and since the
user is only interested in one or two grads when they use this form, each
selection in the combo box clears the dataset the textboxes are bound to and
retrieves exactly one record from the database.

If, instead, I retrieved the entire event I would have worked with the
BindingContext Position. However, that isn't applicable in this case, since
there is only one record.

wr
 
G

Guest

Well, then, I guess the solution you already found is probably the best one
to use (dropping the bindings and then adding them back).

You might want to consider adding Bind, UnBind and ReBind methods to your
base classes (along with your own IBind interface). This makes it much easier
when you have a lot of controls on your form, because those methods, at a
Form level would call the methods in it's Controls collection for those
controls that implement the IBind interface. You wouldn't have to do it for
each TextBox (or whatever) yourself, you'd simply issue the Form UnBind and
ReBind, and it would handle it all for you.

~~Bonnie
 
G

Guest

Actually, though, I just had another thought. If you're simply clearing the
DataSet, that shouldn't mess up your binding (although I haven't messed with
it) ... how are you adding the new row back in?

~~Bonnie
 
G

Guest

OK, I've played around with it a bit, just to be sure. This should work, so
you're missing something somewhere. You showed how the text box databinding
looks in the IDE, but how does it actually look in the code? (And also, my
other question is still pertinent ... how are you adding in the new row?)

~~Bonnie
 
G

Guest

I simply clear the dataset and refill it. Here is the code that does that.
The only change that makes this work is the clear and adding of binding
before and after the merge.

Sub FillEventPerson()

DsEventPerson1.Clear()
With Me
.tbFName.DataBindings.Clear()
.tbLName.DataBindings.Clear()
.tbEmail.DataBindings.Clear()
.tbNotes.DataBindings.Clear()
End With

DsEventPerson1.Merge(wsCommon.GetSpecificPerson(cmbSubject.Value))

With Me
.tbFName.DataBindings.Add("Text", DsEventPerson1.tblSubject, "FirstName")
.tbLName.DataBindings.Add("Text", DsEventPerson1.tblSubject, "LastName")
.tbEmail.DataBindings.Add("Text", DsEventPerson1.tblSubject, "Email")
.tbNotes.DataBindings.Add("Text", DsEventPerson1.tblSubject, "Notes")
End With
'rest of this code does not apply to this problem
RowCountB = 0
CurrentRowB = 0
Dim ds As New wsOrgEvent.dsSubjectFoto
ds.Merge(wsCommon.GetSpecificFotos(cmbSubject.Value))
dtFotosB = ds.tblSubjectFoto
RowCountB = dtFotosB.Rows.Count
Call InitializeDisplay()
ds = Nothing
End Sub
 
G

Guest

I simply added a new Row to my DataTable after clearing it. Since you're
attempting to use the .Merge() method, could you tell me what your
wsCommon.GetSpecificPerson(cmbSubject.Value) returns? Rows? DataSet?
DataTable? I suspect the problem probably lies here somewhere.

~~Bonnie
 
G

Guest

it returns a dataset. Here is the code for that:

<WebMethod()> Public Function GetSpecificPerson(ByVal intPerson_ID As Int32)
As dsEventPerson
Dim ds As New dsEventPerson
'Get Subjects
daSpecificPerson.SelectCommand.Parameters("@Person_ID").Value = intPerson_ID
daSpecificPerson.Fill(ds, "tblSubject")

'Get Subject Addresses
daSpecificAddress.SelectCommand.Parameters("@Person_ID").Value = intPerson_ID
daSpecificAddress.Fill(ds, "tblSubjectAddress")

'Get Associates
daSpecificAssociate.SelectCommand.Parameters("@AssociatedSubject_ID").Value
= intPerson_ID
daSpecificAssociate.Fill(ds, "tblAssociate")

'Get Associate Addresses
daSpecificAssociateAddress.SelectCommand.Parameters("@AssociatedSubject_ID").Value = intPerson_ID
daSpecificAssociateAddress.Fill(ds, "tblAssociateAddress")
Return ds
End Function

wr
 
G

Guest

Hmmm ... still should work. At least it does when I try it.

I poked around some more, on a hunch about the two different ways to add a
DataBinding, and I hit pay dirt. Here's what's going on ... you mentioned
something in an earlier post about how the DataBinding looked in the IDE
(PropertySheet, I assume), but didn't actually post what that looked like. I
think, maybe, that when the form initially Loads, that your DataBinding to
your TextBoxes, is of this format:

Me.tbFName.DataBindings.Add("Text", DsEventPerson1, "tblSubject.FirstName")

instead of this format:

Me.tbFName.DataBindings.Add("Text", DsEventPerson1.tblSubject, "FirstName")

Note that this is actually two different DataBindings. I always use the
second version of it (as you have when you re-bind), because of problems I've
run into in the past with the first version. I think the first version of it
doesn't work correctly when Merging DataSets. I just tried it that way and
sure enough, it didn't show up correctly in the bound TextBox.

So, bottom line ... don't do the DataBinding in the PropertySheet, code it
by hand (using the second version) and everything should be fine. You should
then not have to unbind and rebind each time.

~~Bonnie
 
G

Guest

Regarding:
You might want to consider adding Bind, UnBind and ReBind methods to your
base classes (along with your own IBind interface).

I am just now venturing into full object-oriented programming, while I
understand your suggestion conceputally, I am a little ways off from being
able to implement this. Do you have existing code to illustrate this. I can
read C#.

Again, I appreciate your time on this.

WR
 
G

Guest

Worked like a champ.

did you see my earlier response?

Regarding:
You might want to consider adding Bind, UnBind and ReBind methods to your
base classes (along with your own IBind interface).

I am just now venturing into full object-oriented programming, while I
understand your suggestion conceputally, I am a little ways off from being
able to implement this. Do you have existing code to illustrate this. I can
read C#.

You have spent too much time on this as is -- so only if you have something
already done.

WR
 
G

Guest

I typically keep text files of answers I've posted for FAQs, and I have some
examples of DataBinding methods, but I've not included stuff with
Unbind/Rebind examples. However, it might be worthwhile to throw something
together for that. Gimme a few days to get back to you on this ... if it
seems like I've forgotten after a few days, feel free to remind me ... I just
gotta find some extra spare time to cobble something together.

~~Bonnie
 
R

Ravichandran J.V.

1. Clearing the dataset and refilling it does not sound like a good
design to me.

2. A ComboBox has a ValueField which exposes the value for a selected
item.

3. You can databind a control to a datasource with the following code

txt1.DataBindings.Add("Text",ds.Tables("tblSubject"),"FirstName")

To move the pointer to the next row, use

txt1.Position+=1

But for this databinding to work, you should not clear and refill the
dataset.


with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
G

Guest

IMHO, there's nothing wrong with clearing and refilling the DataSet. That is
not what his problem was anyway. The problem was DataBinding incorrectly to
begin with. If you'll read the entire thread, you'll see what I mean.

~~Bonnie
 
G

Guest

As I stated earlier in the thread, bringing down 800 records to look at only
one or two records, would be a poor solution. Especially when you have a
dozen or more users doing this on different events. Sometimes it is not
efficient to bring down the whole set and select on the client. Although,
that is always my first consideration. Different situations demand different
solutions.

WR
 

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