dcsimg
Results 1 to 8 of 8

Thread: Binding a textbox to a code built datagrid

  1. #1

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    36

    Binding a textbox to a code built datagrid

    Hi,

    I am trying to bind a textbox databindings (text) to the select field of my datagridview i have done this in code rather than the GUI because of my SQL select statment.

    As my PracticeDB is quite old records from 05/12/2014 i have had to use a sepecified date. But i plan to use the current date and return that in my query.

    Here is my code:

    Code:
        Private Sub Form11_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim Sqlcon As SqlConnection = New SqlConnection("Data Source=JAMESPC\SUPPORTDB;Initial Catalog=Support_DB;Persist Security Info=True;User ID=username;Password=password")
            Dim QueryOpen As String = "Select * from ['Job info$'] where [Due on site] > Convert(datetime, '05/12/2014') AND [Job Completed?]= 0 order by [Job Number] desc"
            Dim DataAdpt As New SqlDataAdapter(QueryOpen, Sqlcon)
            Dim ds As New DataSet()
            Sqlcon.Open()
            DataAdpt.Fill(ds, "['Job_Info$']")
            Sqlcon.Close()
            DataGridView1.DataSource = ds
            DataGridView1.DataMember = "['Job_Info$']"
            txtcust.DataBindings.Add("Test", ds, "Customer")
            'txtequip.DataBindings.Add("Text", ds, "[Type of Equipment]")
        End Sub
    The datagrid populates correctly, but on running
    Code:
    txtcust.DataBindings.Add("Test", ds, "Customer")
    It errors woth Cannot bind to the property or column Customer on the DataSource.

    I maybe doing this wrong but my plan is once i have some more updated records in it my SQL query will be something like

    Code:
     
    Dim QueryOpen As String = "Select * from ['Job info$'] where [Due on site] > Current Date AND [Job Completed?]= 0 order by [Job Number] desc"
    Thanks James
    Last edited by VBJames; Jun 13th, 2019 at 10:19 AM.

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,013

    Re: Binding a textbox to a code built datagrid

    Though your description contradicts your code, or in the least, makes very little sense, the problem is youre not binding to a table, youre binding to a dataset.
    Code:
    txtcust.DataBindings.Add("Test", ds, "Customer")
    should be
    Code:
    txtcust.DataBindings.Add("Test", ds.Tables("['Job_Info$']"), "Customer")

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,728

    Re: Binding a textbox to a code built datagrid

    Don't create a DataSet in the first place. This is something that really bugs me but SOOOOOOO may people just create a DataSet for no reason. If you're only going to populate a single DataTable in the DataSet then what's the DataSet for? Just create a DataTable and populate that. You should then bind that to a BindingSource and then bind that to your controls.

  4. #4

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    36

    Re: Binding a textbox to a code built datagrid

    apologies sorry for the bad description, what about changing the textbox once a new record is selected as my query is pulling 4 records in and would like the textbox's to show the values on the selected record.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,728

    Re: Binding a textbox to a code built datagrid

    Quote Originally Posted by VBJames View Post
    apologies sorry for the bad description, what about changing the textbox once a new record is selected as my query is pulling 4 records in and would like the textbox's to show the values on the selected record.
    You don't have to do anything. Data-binding does that for you. If you bind the same data source to both the grid and the individual controls then the individual controls will reflect the selection in the grid and the grid will reflect any changes in the individual controls. Just be sure to call Validate on the form and EndEdit on the BindingSource before saving to ensure any pending edits have been committed to the data source.

  6. #6

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    36

    Re: Binding a textbox to a code built datagrid

    jmcilhinney has this not been done already in the code as my datagridview datasource = ds and i am referencing ds in my bindings.

    Code:
        Private Sub Form11_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim Sqlcon As SqlConnection = New SqlConnection("Data Source=JAMESPC\SUPPORTDB;Initial Catalog=Support_DB;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD")
            Dim QueryOpen As String = "Select * from ['Job info$'] where [Due on site] > Convert(datetime, '05/12/2014') AND [Job Completed?]= 0 order by [Job Number] desc"
            Dim DataAdpt As New SqlDataAdapter(QueryOpen, Sqlcon)
            Dim ds As New DataSet()
            Sqlcon.Open()
            DataAdpt.Fill(ds, "['Job_Info$']")
            Sqlcon.Close()
            DataGridView1.DataSource = ds
            DataGridView1.DataMember = "['Job_Info$']"
            txtcust.DataBindings.Add("Text", ds.Tables("['Job_Info$']"), "Customer")
            txtequip.DataBindings.Add("Text", ds.Tables("['Job_Info$']"), "Type of Equipment")
            txtmodel.DataBindings.Add("Text", ds.Tables("['Job_Info$']"), "Model")
            txtfault.DataBindings.Add("Text", ds.Tables("['Job_Info$']"), "Fault")
            txtloc.DataBindings.Add("Text", ds.Tables("['Job_Info$']"), "Location")
            txtuser.DataBindings.Add("Text", ds.Tables("['Job_Info$']"), "User")
            txtreport.DataBindings.Add("Text", ds.Tables("['Job_Info$']"), "Reported By")
            txton.DataBindings.Add("Text", ds.Tables("['Job_Info$']"), "Date Reported")
            txtat.DataBindings.Add("Text", ds.Tables("['Job_Info$']"), "Time Reported")
            txtdealingwith.DataBindings.Add("Text", ds.Tables("['Job_Info$']"), "Attended by")
            txtaction.DataBindings.Add("Text", ds.Tables("['Job_Info$']"), "Type of Equipment")
    
        End Sub

    Thank's James

  7. #7

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    36

    Re: Binding a textbox to a code built datagrid

    Sorted it.......

    Removed ds.Tables("['Job_Info$']") & changed DataGridView1.DataSource = ds to DataGridView1.DataSource = ds.Tables("['Job_Info$']")

    Thanks for everyone's input and assistance.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,728

    Re: Binding a textbox to a code built datagrid

    You should bind the DataSet/DataTable to a BindingSource and then bind that to your controls.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width