Missing DataBind Method For DataGrid

Posted on 2004-11-21
Last Modified: 2010-04-24
It seems that do an update from the datagrid back to the datbase I need the .DataBind method for the DataGird.  This option does not appear in my VB.Net 2003???  The closest I have is .DataBindings but it appears not to be the same function?

Any help would be greatly appreciated.  I am trying to add a DataSet to an existing DataGrid and DataSet, and then update the database.  Shown below is what I have but does not alter the database in any way.

        connectionstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ProjectFolder & "\" & ProjectName & "\points.mdb"

        Dim dfins As New DataSet

        Dim confin As OleDb.OleDbConnection = New OleDb.OleDbConnection(connectionstr)

        Dim dafin As New OleDb.OleDbDataAdapter("select * from CoordList", confin)


        MainDataGrid.DataSource = ds1.Tables(0)

        Dim cm As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(dafin)

Question by:toshea
    LVL 25

    Expert Comment

    If this is a windows app, then there is no databind for the datagrid because it isn't necessary, databind is only for web apps.

    where is the above code located because if this is in the same sub, of course your database isn't updated because you haven't made any changes to it.

    Author Comment

    What I am trying to accomplish is this.  I have an Access database established.  I want to read in a comma delimited file and populate the existing database.  I am trying to do this by creating a datagrid with the imported comma delimted file on a form.  On another form I have a datagrid populated with the database.  Now I would like to transfer the comma delimted datagrid to my database.  I seem to be struggling with this.  Shown below is the code to first populate the imported file datagrid, followed by the code to import the database file datagrid.  I can get these two datagrids populated with this code.  However, I cannot get the two datagrids into my database.  Any thoughts?

    ' Comma Delimited Datagrid On Form1

            Dim strConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
                      & "Data Source=" & strFilePath & ";" & "Extended Properties=""text;HDR=NO;FMT=Delimited"""

                    Dim conn As New OleDb.OleDbConnection(strConnectionString)
                    conn.Open() ' Open connection with the database.

                    Dim objCmdSelect As New OleDb.OleDbCommand("SELECT * FROM [" & strFileName & "]", conn) ' Create new OleDbCommand to return data from worksheet.
                    'Dim objCmdSelect As New OleDb.OleDbCommand("Select * from [Sheet1$]", conn) ' Create new OleDbCommand to return data from worksheet.

                    ' Create new OleDbDataAdapter used to build a DataSet based on the preceding SQL SELECT statement.
                    objAdapter = New OleDb.OleDbDataAdapter

                    'Pass the Select command to the adapter.
                    objAdapter.SelectCommand = objCmdSelect

                    'Create new DataSet to hold information from the worksheet.
                    objDataSet = New DataSet

                    'Fill the DataSet with the information from the file.
                    objAdapter.Fill(objDataSet, "CoordList")

                    ' Create Primary Key
                    Dim myTable As DataTable = New DataTable
                    Dim keys(1) As DataColumn
                    Dim myColumn As DataColumn

                    ' Create column one.
                    myColumn = New DataColumn
                    myColumn.DataType = System.Type.GetType("System.String")
                    myColumn.ColumnName = "Point"

                    ' Add the column to the DataTable Columns collection.

                    ' Add the column to the array.
                    keys(0) = myColumn
                    ' End Create Primary Key

                    ' Create TableSet
                    objTableSet = New DataGridTableStyle
                    objTableSet.MappingName = objDataSet.Tables(0).ToString

                    objDataSet.Tables(0).Columns(0).ColumnName = "Point"
                    objDataSet.Tables(0).Columns(1).ColumnName = "Northing"
                    objDataSet.Tables(0).Columns(2).ColumnName = "Easting"
                    objDataSet.Tables(0).Columns(3).ColumnName = "Elevation"
                    objDataSet.Tables(0).Columns(4).ColumnName = "Description"
                    objDataSet.Tables(0).Columns(5).ColumnName = "Layer"
                    objDataSet.Tables(0).Columns(6).ColumnName = "Method"
                    objDataSet.Tables(0).Columns(7).ColumnName = "Notes"

                    Dim TC0 As New DataGridTextBoxColumn
                    TC0.MappingName = objDataSet.Tables(0).Columns(0).ColumnName
                    TC0.HeaderText = "Point"
                    TC0.Width = 80
                    TC0.TextBox.MaxLength = 10

                    Dim TC1 As New DataGridTextBoxColumn
                    TC1.MappingName = objDataSet.Tables(0).Columns(1).ColumnName
                    TC1.HeaderText = "Northing"
                    TC1.Width = 80
                    TC1.TextBox.MaxLength = 30

                    Dim TC2 As New DataGridTextBoxColumn
                    TC2.MappingName = objDataSet.Tables(0).Columns(2).ColumnName
                    TC2.HeaderText = "Easting"
                    TC2.Width = 80
                    TC2.TextBox.MaxLength = 30

                    Dim TC3 As New DataGridTextBoxColumn
                    TC3.MappingName = objDataSet.Tables(0).Columns(3).ColumnName
                    TC3.HeaderText = "Elevation"
                    TC3.Width = 80
                    TC3.TextBox.MaxLength = 30

                    Dim TC4 As New DataGridTextBoxColumn
                    TC4.MappingName = objDataSet.Tables(0).Columns(4).ColumnName
                    TC4.HeaderText = "Description"
                    TC4.Width = 120
                    TC4.TextBox.MaxLength = 50

                    Dim TC5 As New DataGridTextBoxColumn
                    TC5.MappingName = objDataSet.Tables(0).Columns(5).ColumnName
                    TC5.HeaderText = "Layer"
                    TC5.Width = 120
                    TC5.TextBox.MaxLength = 50

                    Dim TC6 As New DataGridTextBoxColumn
                    TC6.MappingName = objDataSet.Tables(0).Columns(6).ColumnName
                    TC6.HeaderText = "Method"
                    TC6.Width = 120
                    TC6.TextBox.MaxLength = 50

                    Dim TC7 As New DataGridTextBoxColumn
                    TC7.MappingName = objDataSet.Tables(0).Columns(7).ColumnName
                    TC7.HeaderText = "Notes"
                    TC7.Width = 200
                    TC7.TextBox.MaxLength = 200

                    ' Add The TableStyle To The Datagrid

                    'Build a table from the original data.
                    ImportedGrid.DataSource = objDataSet.Tables(0).DefaultView

                    'Clean up objects.

    ' Database Datagrid On Form2

            Dim connectionstr As String
            Dim mysql As String

            connectionstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ProjectFolder & "\" & ProjectName & "\points.mdb"

            Dim dfins As New DataSet

            Dim confin As OleDb.OleDbConnection = New OleDb.OleDbConnection(connectionstr)

            Dim dafin As New OleDb.OleDbDataAdapter("select * from CoordList", confin)


            DataGrid1.DataSource = objDataSet.Tables(0)

            Dim cm As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(dafin)


    ' To Merge Datagrids And To Write The Database
    ' Button On Form2

            Dim myConnection As OleDbConnection
            Dim mysql As String

            myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ProjectFolder & "\" & ProjectName & "\points.mdb")

            Dim connectionstr As String


            mysql = "SELECT * FROM CoordList"

            Dim da As New OleDbDataAdapter(mysql, daConnection)

            Dim ds As New DataSet("CoordList")

            ' Map Table to CoordList
            da.TableMappings.Add("Table", "CoordList")

            da.Fill(ds, "CoordList")



            Import_Grid.DataGrid1.DataSource = ds.Tables("CoordList").DefaultView


            GoTo bypass

    LVL 25

    Expert Comment

    Hi toshea,

    If you just want to add the delimited file to your database, there is a much simpler way to do this.

    Is there a special reason you want to use a datagrid to accomplish this ?

    Author Comment

    There was at one point, to cross check grids and warn of duplicates and request action.  At this point I would rather just have the data transfered right to the database.  I would appreciate your method of accomplishing this.
    LVL 25

    Accepted Solution

    here is an example (this works I tested it) this ofcourse only works if the text file and the table have the same structure.
    the trick is to update the target dataset with the dataset that you filled with the text file

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim strfilepath As String = "c:\"
                Dim strConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
                         & "Data Source=" & strFilePath & ";" & "Extended Properties=""text;HDR=NO;FMT=Delimited"""

                Dim conn As New OleDb.OleDbConnection(strConnectionString)

                Dim objCmdSelect As New OleDb.OleDbCommand("SELECT * FROM [test.txt]", conn)

                objadapter = New OleDb.OleDbDataAdapter

                objadapter.SelectCommand = objCmdSelect

                objdataset = New DataSet

                objadapter.AcceptChangesDuringFill = False
                objdataset.Tables(0).Columns(0).ColumnName = "tt1"
                objdataset.Tables(0).Columns(1).ColumnName = "tt2"
                objdataset.Tables(0).TableName = "ttt"

                targetDS = New DataSet

            Catch ex As Exception
            End Try
        End Sub

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    746 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

    15 Experts available now in Live!

    Get 1:1 Help Now