How to bind a cell in a datagrid to a field in a database table

Posted on 2006-04-24
Last Modified: 2010-04-23
I'm looking to bind a cell in a data grid to a database field in a database table. I'm using an ODBC Connection to connect to a local access database.
Question by:carter_jl
    1 Comment
    LVL 6

    Accepted Solution


                Dim nColCombo1 As New DataGridViewComboBoxColumn
                With nColCombo1
                    .HeaderText = "Item PartNo"
                    .Name = "Col_ItemPartNo"
                    Result = clsData.FillCombo(nColCombo1, "Items", "ItemId", "ItemPartNo", "Select * From Items ")

                    .Width = 100
                    .DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton
                    .AutoComplete = True
                    .Visible = True

                End With : dGrid.Columns.Add(nColCombo1)

    here is my function :

     Public Function FillCombo(ByRef cBox As Object, ByVal Tbl As String, ByVal IdFld As String, ByVal NameFld As String, ByVal SqsString As String) As String

                ' cBox ur ComboBox    
                ' v_ConnectionString Connection String
                ' Tbl  ur table ( Or View ) name
                ' IdFld  DisplayMember Field
                ' NameFld DisplayMember Field
                ' SqsString Select statment

                Dim Conn As SqlConnection = New SqlConnection(v_ConnectionString)
                Dim Comm As SqlCommand = New SqlCommand(SqsString, Conn)
                Comm.Connection = Conn
                Comm.CommandText = SqsString ' "commandtorun"
                Comm.CommandType = CommandType.Text
                Dim DS As DataSet = New DataSet()

                Dim DA As New SqlDataAdapter(Comm)
                DA.Fill(DS, Tbl)

                Dim dt As New DataTable
                dt.Columns.Add(NameFld, GetType(System.String))
                dt.Columns.Add(IdFld, GetType(System.String))

                With cBox
                    .DataSource = DS.Tables(Tbl)  
                    .DisplayMember = NameFld
                    .IdFld = IdFld
                End With

                Call ClossConn(Conn) ' Closs Current Connection

                Return 0
            Catch ex As Exception

                Return ex.Message

            End Try
        End Function

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now