DataGridView Binding

pitaridis

Well-known member
Joined
Nov 18, 2005
Messages
63
Programming Experience
10+
Hi,

I would like to know how to bind a DataGridView in such a was so that when I click on a record of the DataGridView, automaticaly the current record of the table stored in the DataSet will be set to the selected value.

In my program I want to bind the DataGridView and some textboxes with the same table and when I click on a record, the textboxes will be updated with the equivalent value of the selected record.

Thanks for your help.
 
Can someone help me? I have attacked the project so that you will understand exactly what I want to do. I just want the DataGridView to cooperate with the textbox. When I press the next, previous, last, first buttons the selected record of the DataGridView will be changed to the equivalent record. This means that if I change the current record of the DataGridView by clicking on it, the company textbox will show the company of the selected record.
 

Attachments

  • CM School Manager.zip
    205.9 KB · Views: 19
If the DGV and the textboxes bind through the same bindingsource, then moving the current row of the DGV by clicking on a cell, will alter the bindingsource position and hence update the text boxes
 
When you upload a project to here, please delete the BIN and OBJ folders first; they are a waste of space and bandwidth because VS regenerates them, and they contain bulky binary code that is no interest to a human. WHen I deleted your folders, the zip file became 21Kb.. the board owner will thank you for not wasting his bandwidth if you can remember to remove the BIN and OBJ folders in future. Thanks
 
I attempted to open your project, but the solution file "was reated by a newer version of this application and cannot be opened"

Please adjust your profile to reflect the actual version of .NET youre using, because it's newer than my 2005 / 2.0

If you want my help beyond the "bind through same bindsource" advice, you'll need to save teh project in whatever 2005 compatibility mode you have available
 
I can not save the project as 2005 but I have here the code of my form.

VB.NET:
Public Class Form1
    Dim daCustomers As New OleDb.OleDbDataAdapter
    Dim dsCustomers As New DataSet

    Dim MyConnection As OleDb.OleDbConnection

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim cmdCustomers As New OleDb.OleDbCommand
        Dim DatabasePath As String = System.IO.Directory.GetCurrentDirectory & "\Database.mdb"
        Try
            MyConnection = New System.Data.OleDb.OleDbConnection("Provider=microsoft.jet.oledb.4.0;data source ='" & DatabasePath & "'")
            MyConnection.Open()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "error")
            End
        End Try

        Try
            daCustomers = New OleDb.OleDbDataAdapter("Select * from Contacts", MyConnection)

            ' automatically generate the Update, Insert, and Delete commands
            Dim cb As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(daCustomers)

            daCustomers.Fill(dsCustomers, "Customers")

            txtCompany.DataBindings.Add("Text", dsCustomers.Tables("Customers"), "Company")

            Dim MiEnlazador As New BindingSource
            MiEnlazador.DataSource = dsCustomers.Tables("Customers")


            DataGridView1.DataSource = dsCustomers
            DataGridView1.DataMember = ("Customers")

            UpdatePosition()
        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "frmCustomers_Load")
        End Try
    End Sub

    Private Sub UpdatePosition()
        Me.lblPosition.Text = ((Me.BindingContext(dsCustomers.Tables("Customers")).Position + 1).ToString + " of " + Me.BindingContext(dsCustomers.Tables("Customers")).Count.ToString)
    End Sub

    Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
        UpdatePosition()
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        Try
            Me.BindingContext(dsCustomers.Tables("Customers")).Position = 0
            UpdatePosition()
        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "btnNext_Click")
        End Try
    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        Try
            Me.BindingContext(dsCustomers.Tables("Customers")).Position -= 1
            UpdatePosition()
        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "btnNext_Click")
        End Try
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Try
            Me.BindingContext(dsCustomers.Tables("Customers")).Position += 1
            UpdatePosition()
        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "btnNext_Click")
        End Try
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        Try
            Me.BindingContext(dsCustomers.Tables("Customers")).Position = Me.BindingContext(dsCustomers.Tables("Customers")).Count - 1
            UpdatePosition()
        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "btnNext_Click")
        End Try
    End Sub

    Private Sub btnAddRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRecord.Click
        Try
            Me.BindingContext(dsCustomers.Tables("Customers")).EndCurrentEdit()
            Me.BindingContext(dsCustomers.Tables("Customers")).AddNew()
            UpdatePosition()
        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "btnNext_Click")
        End Try
    End Sub
End Class

Please help me. I am trying to solve this problem for 5 days now but I haven't solved it yet.
 
Ahh.. *shakes head*

Please go to the DW2 link in my signature, read the article about Creating a Simple Data App.. It will teach you proper data access.. this is old, pointless, labour intensive, 5 days way.. New way is 5 minutes painless and better coded all round.. IDE writes most the boring code for you
 
Also.. please dont tell lies about what version of .NET you use! We need this info to be accurate.. go to your profile control panel now and change the platform to your proper one!
 
I did not lied. Two weeks ago I installed visual studio 2008. I didn't know that I had defined a previous version of visual studio. Sorry about this.
 
Back
Top