none
Databinding RRS feed

  • Question

  • Hello all

    I'm programming in VB.net and use most of time a access database. I have look already long time for information about databinding but can't find the complete information. So I know how to use on text box that I must use text as value. Fe txtTextbox.Databindings.Add("Text", tabelname, "fieldname")

    Now I like to know when use Text, when value or what is more. I have also a field give True and false and bind to checkbox. Please litle more info would be nice. Even a link where I can find it good is nice

     thanks

    Stefan Groenen

    Wednesday, January 15, 2020 4:03 AM

Answers

  • Hello,

    Here is how to think about data binding. Ask yourself, if I were to set the state of a CheckBox what would it be in the property window of a CheckBox? Checked right :-) Same say for a DateTimePicker, the property you would set in the property window is Value so when data binding Value is the member.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by sgroenen Thursday, January 16, 2020 8:35 AM
    Wednesday, January 15, 2020 11:10 AM
    Moderator

All replies

  • Hi sgroenen, 

    Thank you for posting here.

    According to your description, I make a test which bind class and datatable to textbox and checkbox.

    Here’s the code you can refer to:

        Public Class Person
            Public Property Name As String
            Public Property Age As Integer
            Public Property Judge As Boolean
        End Class
    
        Private dt As DataTable = New DataTable()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt.Columns.Add("Name", GetType(String))
            dt.Columns.Add("Age", GetType(Integer))
            dt.Columns.Add("Judge", GetType(Boolean))
            dt.Rows.Add("B", 14, False)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim p As Person = New Person() With {
                .Name = "A",
                .Age = 12,
                .Judge = True
            }
            TextBox1.DataBindings.Add("Text", p, "Name")
            CheckBox1.DataBindings.Add("Checked", p, "Judge")
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            TextBox1.DataBindings.Clear()
            CheckBox1.DataBindings.Clear()
            TextBox1.DataBindings.Add("Text", dt, "Name")
            CheckBox1.DataBindings.Add("Checked", dt, "Judge")
        End Sub
    

    Result:

    Besides, you can also use following code to bind data to your control.

            Dim binding As BindingSource = New BindingSource()
            binding.DataSource = dt
            TextBox1.DataBindings.Add("Text", binding, "Name")
            'Or
            TextBox1.DataBindings.Add(New Binding("Text", binding, "Name"))
    

    Hope it can help you.

    Best Regards,

    Xingyu Zhao




    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 15, 2020 7:54 AM
    Moderator
  • Hello,

    Here is how to think about data binding. Ask yourself, if I were to set the state of a CheckBox what would it be in the property window of a CheckBox? Checked right :-) Same say for a DateTimePicker, the property you would set in the property window is Value so when data binding Value is the member.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by sgroenen Thursday, January 16, 2020 8:35 AM
    Wednesday, January 15, 2020 11:10 AM
    Moderator
  • Stefan, 

    You have to look what property in the control is used when the user change a value. 

    In a Textbox that is the property "Text". 

    In a checkbox that is the property "Value". "Text" in a checkbox is the name displayed with the checkbox and that is something the user cannot even change. 


    Success
    Cor

    Wednesday, January 15, 2020 12:55 PM