Databinding datagridview values to a textbox

    Question by:


    Hi Experts!  I need help databinding values from a datagridview to textboxes that the user could make changes and update. The datagridview1 is readonly. I orginally put this code in the datagridview1_RowEnter:
    Me.txtEmployee.Text = .Cells("User_ID").Value
    Me.txtCreateDate.Text = .Cells("Create_Date").Value
    Me.txtSubmitDate.Text = .Cells("Submit_Date").Value

    but I am pretty sure that will not allow me to update the dataset for the datagridview. Any Ideas?

    Good Question?

    Verified Answer?

    The member who asked this question verified this comment provided the solution that solved their problem.

    by:Posted on 2008-04-02 at 12:27:03ID: 21266494

    Absolutely!  You're changing the DataSource right there, and that's why they are not in synch!  
    (Not to mention that if there is 0 rows, there won't be any data.)

    What you need to do is:

    Private Sub cbEmployee_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbEmployee.SelectedIndexChanged

            Dim bs As BindingSource = Me.DataGridView1.DataSource    'get the bindingsource

            ' Assigns User_ID to the userid
            userid = DirectCast(cbEmployee.SelectedItem, DataRowView).Item("User_ID")
                userid = DirectCast(cbEmployee.SelectedItem, DataRowView).Item("User_ID")
                'loads the datagridview with the information from the Master Report by Userid selected

                dsv = LoadMaster(userid)
                bs.DataSource = dsv    'reset the datasource

                'DataGridView1.DataSource = dsv.Tables(0).DefaultView  <should not be needed

            Catch ex As Exception

            End Try
        End Sub

    That should work, let me know if you have any problems.

    This content is available to Experts Exchange members

    See the answer now
    with your Free 30 Day Trial

    Get unlimited access to solutions & experts

    • 4,169,477 solved questions
    • 3,805 articles & videos
    • 15,413 tech experts

    Get Access Now

    Ask Your Tech Question. Get Expert Solutions.We will email you when an expert has commented on your question.

    We will never share this with anyone. Privacy Policy Terms of Use

    Select topics

    You may select up to five topics.

    Top Expert Contributor

    Essential articles and videos from the Experts

    More valuable questions with Expert answers


    RELATED TOPICS view all topics

    1. ASP.NET
    2. C#
    3. Visual Basic.NET
    4. MS Development
    5. Editors IDEs
    6. Visual Basic Classic
    7. MS SQL Server
    8. MS SQL Server 2005
    9. Programming
    10. JavaScript