Results 1 to 8 of 8

Thread: [RESOLVED] Binding datarow to textbox.

  1. #1

    Thread Starter
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,248

    Resolved [RESOLVED] Binding datarow to textbox.


    I have the following code where I'm trying to bind a datarow to a textbox.
    Code:
    Me.TierNameText.DataBindings.Add("Text", Me.m_TierRecord, "tier_name")
    Problem is, when the form is displayed no text is shown in the textbox.

    This code runs before I call the form's ShowDialog method. I stepped through the code to make sure the datarow has data, and indeed it does.

    I rarely use databinding but thought I'd try it this time. I'm obviously doing something wrong, though.

    Any ideas?
    I always add to the reputation of those whose post are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,528

    Re: Binding datarow to textbox.

    Not sure why you are not seeing the data. Try the example below, see if it will assist you in properly setting up your TextBox. In short the textbox is not bound to a single row but a series of rows.

    Code:
    Public Class Form1
        '
        ' Requires 
        ' 1 DataGridView, 1 TextBox
        '
        WithEvents bsOrders As New BindingSource
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
            LoadDataTable()
        End Sub
        Private Sub LoadDataTable()
            Dim dtOrders As New DataTable("Orders")
    
            dtOrders.Columns.Add("Employee", GetType(String))
            dtOrders.Columns.Add("OrderID", GetType(Integer))
            dtOrders.Columns.Add("Amount", GetType(Decimal))
    
            dtOrders.Rows.Add(New Object() {"Sam", 5, 25.0})
            dtOrders.Rows.Add(New Object() {"Kevin", 100, 1000.0})
            dtOrders.Rows.Add(New Object() {"Sue", 9, 11.0})
            dtOrders.Rows.Add(New Object() {"Tom", 12, 7.0})
            dtOrders.Rows.Add(New Object() {"Bob", 12, 512.0})
    
            bsOrders.DataSource = dtOrders
            bsOrders.Sort = "Employee"
    
            DataGridView1.DataSource = bsOrders
            DataGridView1.Columns(2).AutoSizeMode = _
                DataGridViewAutoSizeColumnMode.Fill
    
            TextBox1.DataBindings.Add("Text", bsOrders, "Amount")
        End Sub
    End Class

  3. #3

    Thread Starter
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,248

    Re: Binding datarow to textbox.

    I tried changing the bindingsource to
    Code:
    Me.TierNameText.DataBindings.Add("Text", Me.m_TierRecord.Table, "tier_name")
    That gives me the databinding, but it is not bound to the record I want, but rather the first record in the table.

    So it looks like a record itself cannot be bound. I'm going to have to rethink my model somewhat.

    Thanks for your help - that cleared things up for me.
    I always add to the reputation of those whose post are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,528

    Re: Binding datarow to textbox.

    Quote Originally Posted by dolot View Post
    I tried changing the bindingsource to
    Code:
    Me.TierNameText.DataBindings.Add("Text", Me.m_TierRecord.Table, "tier_name")
    That gives me the databinding, but it is not bound to the record I want, but rather the first record in the table.

    So it looks like a record itself cannot be bound. I'm going to have to rethink my model somewhat.

    Thanks for your help - that cleared things up for me.
    Add this to the code I gave already, this will position you to a specific row.
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim Position As Integer = bsOrders.Find("Employee", "Sue")
            If Position <> -1 Then
                bsOrders.Position = Position
            End If
    
        End Sub

  5. #5

    Thread Starter
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,248

    Re: [RESOLVED] Binding datarow to textbox.

    I thought about that option, but ultimately decided to skip trying to do databinding in this instance. I'm just going to manage the reading and writing of the values manually - it's only three fields in a very simple interface.

    Thanks again.
    I always add to the reputation of those whose post are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,528

    Re: [RESOLVED] Binding datarow to textbox.

    Also review the following that keeps the current row in the DataGridView after a sort along with allowing you to keep track of the current record via the BindingSource Position change event.


    Code:
    Public Class Form1
        '
        ' Requires 
        ' 1 DataGridView, 1 TextBox, 1 Button
        '
        WithEvents bsOrders As New BindingSource
        Private mEmployee As String = ""
    
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
            LoadDataTable()
        End Sub
        Private Sub LoadDataTable()
            Dim dtOrders As New DataTable("Orders")
    
            dtOrders.Columns.Add("Employee", GetType(String))
            dtOrders.Columns.Add("OrderID", GetType(Integer))
            dtOrders.Columns.Add("Amount", GetType(Decimal))
    
            dtOrders.Rows.Add(New Object() {"Sam", 5, 25.0})
            dtOrders.Rows.Add(New Object() {"Kevin", 100, 1000.0})
            dtOrders.Rows.Add(New Object() {"Sue", 9, 11.0})
            dtOrders.Rows.Add(New Object() {"Tom", 12, 7.0})
            dtOrders.Rows.Add(New Object() {"Bob", 12, 512.0})
    
            bsOrders.DataSource = dtOrders
            bsOrders.Sort = "Employee"
    
            bsOrders_PositionChanged(Nothing, Nothing)
    
            DataGridView1.DataSource = bsOrders
            DataGridView1.Columns(2).AutoSizeMode = _
                DataGridViewAutoSizeColumnMode.Fill
    
            TextBox1.DataBindings.Add("Text", bsOrders, "Amount")
        End Sub
    
        Private Sub bsOrders_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bsOrders.PositionChanged
            mEmployee = CType(bsOrders.Current, DataRowView).Item("Employee").ToString()
            Console.WriteLine("Current employee is [{0}]", mEmployee)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles Button1.Click
    
            Dim Position As Integer = bsOrders.Find("Employee", "Sue")
            If Position <> -1 Then
                bsOrders.Position = Position
            End If
    
        End Sub
    
        Private Sub DataGridView1_Sorted(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Sorted
            bsOrders.Position = bsOrders.Find("Employee", mEmployee)
        End Sub
    End Class

  7. #7

    Thread Starter
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,248

    Re: [RESOLVED] Binding datarow to textbox.

    Man, you're making me feel terrible. You keep posting stuff to try to help me with binding and I've gone ahead and ditched the idea.
    I always add to the reputation of those whose post are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,528

    Re: [RESOLVED] Binding datarow to textbox.

    Quote Originally Posted by dolot View Post
    Man, you're making me feel terrible. You keep posting stuff to try to help me with binding and I've gone ahead and ditched the idea.
    Personally BindingSource are the way to go. Here is one more nudge to keep you with BindingSource

    Access the underlying DataTable of the BindingSource.DataSource where columns are accessed via column index or column name
    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles Button2.Click
    
            For Each row As DataRow In DirectCast(bsOrders.DataSource, DataTable).Rows
                Console.WriteLine("Employee [{0}] Amount [{1}]", _
                                  row(0).ToString, row("Amount"))
            Next
    
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width