Question How to bind text box to dataset?

DoJa

Member
Joined
Jul 26, 2011
Messages
21
Programming Experience
Beginner
With some help from people on here and some examples I found on Google, I have successfully managed to bind a datagridview to a dataset connected to a mysql database and using the command builder to update any changes back to the database.

This is all well and good, however the datagridview isnt particularly user friendly in GUI terms so I'm attempting to create a more intuitive design using text boxes for data entry instead. I have replaced the datagridview with a text box for each column in the database and have bound the data using the following code, with one line for each box pointing to the appropriate datacolumn:

[XCODE] txtForename.DataBindings.Add("Text", ds.Tables(0), "forename")[/XCODE]

Using this code when I run the program data from the first row in the database displays in the text box as anticipated however when I run ds.update it no longer works as it did with the datagridview and any changes I make in the text box are not comitted to the database.

Where am I going wrong?
 
Firstly, if all you're going to use a DataSet for is to contain a single DataTable then don't use a DataSet. Just use a DataTable. Bind your DataTable to a BindingSource first and then bind that to your control(s). You can then use the MoveNext and MovePrevious methods of the BindingSource to navigate and also associate it with e BindingNavigator to provide a UI. When you want to save, make sure that you call EndEdit on the BindingSource first.
 
Thanks jmcilhinney, that helped a lot! :)

This has fixed all my text inputs.
VB.NET:
 txtForename.DataBindings.Add("Text", bSource, "forename")

I also have a few boolean entries in the database which I would like to control with a 'yes' button and a 'no' button. How can I use the bindingsource as I did with the text inputs to work with buttons? So if there was a 1 in the datarow then the 'yes' button would highlight and if there was a 0 in the datarow then the 'no' button would highlight. Also clicking on the respective button would have to update the change back to the datatable.

Currently I'm using the following:

VB.NET:
 If dsCustomer.Tables("customer").Rows(0).Item("member") = "0" Then
            btnmember.BackColor = Color.CornflowerBlue
            btnmember.BackColor = SystemColors.Control
        ElseIf dsCustomer.Tables("customer").Rows(0).Item("member") = "1" Then
            btnmember.BackColor = Color.CornflowerBlue
            btnmember.BackColor = SystemColors.Control
        End If

And then on the button click event to set the selected change:

VB.NET:
 dsCustomer.Tables("customer").Rows(0).Item("member") = "1"

This essentially does the job, but because it is not using the binding source , when I use bSource.MoveNext() to cycle through each row, those buttons are not getting updated alongside the textboxes. - that is the problem!
 
Last edited:
When you start editing a row through the UI, the BeginEdit method of the BindingSource is called implicitly. As I said in my previous post, you have to call the EndEdit method to commit any changes you make to the underlying data source before saving back to the database. If you want to revert those changes then you can call CancelEdit.
 
I think you misunderstood the question. I read what you said about the Endedit and I am using that and it works with the textboxes. The issue now is how to bind to the buttons, or at least create the effect of doing so.

Is there a way to bind the binding source to a variable which I could then use to set the status of the buttons?

I guess I could just use a bunch of hidden textboxes but it seems a bit 'messy'.
 
Either I was replying to completely the wrong question or you changed your question after I read it. Anyway, for a start, you absolutely should not be using text fields containing "0" and "1" to represent Boolean values. Most databases have a data type dedicated to Boolean values, e.g. 'bit' in SQL Server and 'Yes/No' in Access, that will be automatically mapped to the Boolean data type using ADO.NET. For databases that don't have a dedicated data type, e.g. Oracle, you should at least be using a numeric data type and using the numbers 1 and 0 to represent the values True and False respectively. Absolutely do not use text values.

As for the question, you can't bind anything to a pair of Buttons. You can bind one column/property in a data source to one property of a control. Normally developers use CheckBoxes to represent Boolean values and then they can bind the Checked property. If you're determined to use a pair of Buttons then you have two choices:

1. Create a UserControl to contain the Buttons and expose their state via a single Boolean property that you can then bind to.
2. Don't use data-binding for that column/property of the data source and write code to manage it manually.
 
Back
Top