• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

Missing DataBind Method For DataGrid

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)

        dafin.Fill(ds1)

        MainDataGrid.DataSource = ds1.Tables(0)

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

        dafin.Update(ds1)
       
0
toshea
Asked:
toshea
  • 3
  • 2
1 Solution
 
RonaldBiemansCommented:
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.
0
 
tosheaAuthor Commented:
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.
                myTable.Columns.Add(myColumn)

                ' 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
                objTableSet.GridColumnStyles.Add(TC0)

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

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

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

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

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

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

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

                ' Add The TableStyle To The Datagrid
                ImportedGrid.TableStyles.Add(objTableSet)

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

                'Clean up objects.
                conn.Close()

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 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)

        dafin.Fill(objDataSet)

        DataGrid1.DataSource = objDataSet.Tables(0)

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

        dafin.Update(objDataSet)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 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

        daConnection.Open()

        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")

        ds.Merge(objDataSet)

        da.Update(ds)

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

        daConnection.Close()

        GoTo bypass

0
 
RonaldBiemansCommented:
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 ?
0
 
tosheaAuthor Commented:
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.
0
 
RonaldBiemansCommented:
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:\"
        Try
            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
            objadapter.Fill(objdataset)
            objdataset.Tables(0).Columns(0).ColumnName = "tt1"
            objdataset.Tables(0).Columns(1).ColumnName = "tt2"
            objdataset.Tables(0).TableName = "ttt"

            targetDS = New DataSet
            OleDbDataAdapter1.Fill(targetDS)

            OleDbDataAdapter1.Update(objdataset)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now