I need to load a DataTable from Microsoft Access into a DataSet using OleDb. I need to load that DataSet into a DataGridView. I then need to make changes to the DataGridView and update those changes in the original DataTable in Microsoft Access.
Here is my code so far:
Public tblName As String = "Criteria"
Dim ds As New DataSet()
Dim da As OleDbDataAdapter
Dim cmdBuilder As OleDbCommandBuilder
Dim Bsource As New BindingSource
Public Sub Show_Panel_Manage_Calculations()
Panel_Manage_Calculations.Show()
Nordeen_Investing_3.con.Open()
da = New OleDbDataAdapter("SELECT Calculation, [Interval], Formula FROM " & tblName & "", Nordeen_Investing_3.con)
cmdBuilder = New OleDbCommandBuilder(da)
da.Fill(ds, "Criteria")
Bsource.DataSource = ds
DataGridView_Manage_Calculations.DataSource = Bsource
Nordeen_Investing_3.con.Close()
End Sub
Private Sub Button_Update_Click(sender As Object, e As EventArgs) Handles Button_Update.Click
Nordeen_Investing_3.con.Open()
da.Update(ds, "Criteria")
Nordeen_Investing_3.con.Close()
End Sub
Right now my data from the DataTable is not being displayed in my DataGridView.
The DataSource expects a Table, not the whole DataSet. Also you don't need the BindingSource part. Sample code:
DataGridView_Manage_Calculations.DataSource = ds.Tables(0) 'By assuming that you want the first table
Related
I have DataGridView which doesn't refresh. I was hoping to be able to refresh it by clicking on the 'Refresh' button.
My database is called 'ProjectDatabase' and the table I want to use is called 'SWOT'.
I'v tried a few ways but none have been working, and if it does work then I get new issues like every time I click on refresh it duplicates the data. Im using a MS Acess Database.
I tried the below coding, it refreshes, but duplicates the data x2 every time I click Refresh:
Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
Refreshdata()
End Sub
Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\ahmed\OneDrive\Desktop\ProjectDatabase2003.mdb")
Dim DS As DataSet = New DataSet
Dim DA As OleDbDataAdapter
Dim tables As DataTableCollection = DS.Tables
Dim source1 As New BindingSource()
Private Sub Refreshdata()
DA = New OleDbDataAdapter("Select * from SWOT", myConnection)
DA.Fill(DS, "SWOT_Report")
Dim view1 As New DataView(tables(0))
source1.DataSource = view1
DataGridView1.DataSource = view1
DataGridView1.Refresh()
End Sub
I think that you are overcomplicating your code with the use of the BindingSource, DataSet, DataAdapter, and DataTable member (class scope) variables.
For the member variables, strip them down to these two:
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\ahmed\OneDrive\Desktop\ProjectDatabase2003.mdb"
Dim swotBindingSource As BindingSource
Please note, the myConnection object has been removed and replaced with a Connection String variable instead
In the Forms New() method add:
swotBindingSource = New BindingSource()
DataGridView1.DataSource = swotBindingSource
This will setup your DataGridView to have the swotBindingSource as its DataSource. The BindingSource can be of use later on when it comes to filtering data.
And, finally change the RefreshData sub to be:
Private Sub Refreshdata()
Dim swotDataTable As New DataTable
Using conn As New OleDbConnection(ConnectionString)
Using DA As New OleDbDataAdapter("SELECT * FROM SWOT", conn)
Dim ds As New DataSet
DA.Fill(ds, "SWOT_Report")
swotBindingSource.DataSource = ds.Tables(0)
End Using
End Using
End Sub
When the swotBindingSource gets updated, you should automatically see the updates in the DataGridView.
It is likely you will also have to make other changes to the rest of your code due to the lack of myConnection object, you will be better off creating a new Connection object (similar to how it is done in the RefreshData sub) each time you need to use the database rather than creating one when the application starts and re-using the same connection.
What you need to do is clear your DataTable before filling it again. .NET doesn't do that automatically.
Private Sub Refreshdata()
'-- clear table before filling
If DS.Tables("SWOT_Report") IsNot Nothing Then DS.Tables("SWOT_Report").Rows.Clear
DA = New OleDbDataAdapter("Select * from SWOT", myConnection)
DA.Fill(DS, "SWOT_Report")
Dim view1 As New DataView(tables(0))
source1.DataSource = view1
DataGridView1.DataSource = view1
DataGridView1.Refresh()
End Sub
Private Sub btnAddSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSub.Click
Dim comboboxvalue As String
comboboxvalue = "'" & CBClass.SelectedItem & "'"
Dim sql As String
sql = "Select * From class Where ClassCode=" & comboboxvalue
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=H:\ProjectDatabase.mdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
MyConn = New OleDbConnection
MyConn.ConnectionString = connString
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter(sql, MyConn)
da.Fill(ds, "Class")
Dim view As New DataView(tables(0))
source1.DataSource = view
Form1.dgv.DataSource = view
End Sub
I can currently add one piece of data to the datagrid. When I try to add a second piece of data it replaces the current data stored.
How do I make it so that when I add more data it goes to a new line.
The source code you provided looks a lot like you are just refreshing the data source for the DGV on every btnAddSub click, by assigning to form1.dgv.datasource each time. If you want to simply append new rows to the DGV you will need to manipulate the DGV.Rows collection by adding DataGridViewRow objects to it.
How would I add a filtration function to my program so when I begin typing into a text box, it filters the results in the DataGridView? Just to clarify, this code loads a linked Access database into the DataGridView on form load and allows me to add, remove and update the data contained with in.
I'm relatively new to Visual Basic and I have ran out of ideas in regards to what to in order to get it to work. Thank you in advance for any help provided!
Imports System.Data.OleDb
Public Class frmDatabase
Dim con As New OleDbConnection
Dim ds As New DataSet
Dim dt As New DataTable
Dim da As New OleDbDataAdapter
Private Sub frmDatabase_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Joe\Documents\Visual Studio 2012\Projects\school database viewer\school database viewer\dbSchoolDatabase.mdb"
con.Open()
ds.Tables.Add(dt)
da = New OleDbDataAdapter("Select * from tableStudentDetails", con)
Dim cb = New OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
da.Fill(dt)
dgvStudentDetails.DataSource = dt.DefaultView
con.Close()
End Sub
Private Sub cmdSave_Click_1(sender As Object, e As EventArgs) Handles cmdSave.Click
da.Update(dt)
End Sub
I have a datagridview with datasource an Acces table.
The datagridview is filled and perfect but now I want to create an extra Combobox with a filter possibility.
This is in my load function of the page:
Private Sub ArchiefFacturatie_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'CharelIjsDataSet.tblFactuur' table. You can move, or remove it, as needed.
Me.TblFactuurTableAdapter.Fill(Me.CharelIjsDataSet.tblFactuur)
'Filling comboxbox part skipped
End Sub
At this moment the combobox is filled an the datagridview also.
Now I created an on selectChange action on the combobox.
I had no idea how to tackle this but I've tried it:
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
'Ophalen id artikel
Dim klantid As Integer
klantid = ComboBox1.SelectedValue
Dim klantidstr As String
klantidstr = klantid.ToString
'Via query overige gegevens ophalen
' OLEDB select query
Dim myConnection As OleDbConnection
Dim DBpath As String = "C:\Facturatie\CharelIjs.accdb"
Dim sConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBpath & ";Persist Security Info=True"
myConnection = New OleDbConnection(sConnectionString)
myConnection.Open()
Dim SQLstr As String
SQLstr = "SELECT * FROM tblKlant WHERE KlantID = #id"
Dim cmd As New OleDbCommand(SQLstr, myConnection)
Dim da As New OleDbDataAdapter(cmd)
Dim ds As New DataSet()
cmd.Parameters.Add("#id", OleDbType.VarChar)
cmd.Parameters(0).Value = klantid
Try
da.Fill(ds, "tblKlant")
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Can't load Web page" & vbCrLf & ex.Message)
Return
End Try
DataGridView1.DataSource = ds.Tables("tblKlant")
End Sub
You can get rid of all that code in SelectionChangeCommitted event handler for a start. You have a typed DataSet so use it. Open the DataSet designer by double-clicking the DataSet in the Solution Explorer. You should have a TblKlantTableAdapter adapter. Right-click it and add a new query. Add your filtered query and name the methods FillByKlantID and GetDataByKlantID.
Back in your form designer, add an instance of your table adapter and a BindingSource. Bind the BindingSource to tblKlant in your DataSet and bind your grid to the BindingSource. Now, when the user makes a selection, you first clear the table in the DataSet, then you get the ID from the ComboBox and pass it to a call to that FillByKlantID method. That will get the filtered data from the database and populate the DataTable. Because the DataTable is bound to the BindingSource and that's bound to the grid, the data will be displayed.
I am using VB.NET to pull some data from an SQL database into a Datagridview.
I then want the user to be able to modify the information and save it back to the database, which I have working fine.
What I need to be able to do now is to restrict the values, but way of a combobox for the field Tarrif.
I have configured a DataSource called Tarrif1 and I am using the below code.
I have a couple of issues/questions.
Firstly the dropdown shows a single value of "System.Data.DataViewManagerListItemTypeDescriptor" not the Tarrif1 values.
Secondly, I now have 2 columns on my datatable called Tarrif, the original database one and the one I have added - How can I get the ComboBox to right back to the appropriate Tarrif database field.
Here is my code:
Imports System.Data.SqlClient
Imports System.Data.Common
Public Class ViewCustomersForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=barry-laptop\SQLEXPRESS; database=BillingReferenceData; integrated security=yes"
Dim sql As String = "SELECT * FROM Customers"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim comm As SqlCommand = New SqlCommand(Sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
Private Sub ViewCustomersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'---open the connection and fill the dataset---
conn.Open()
dataadapter.Fill(ds, "Customers_table")
conn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers_table"
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Tarrifs"
'---data bind it---
comboBoxCol.DataSource = Tarrifs1
'comboBoxCol.DisplayMember = "Tarrif" // when I add these rows the new Tarrif column is not visible
'comboBoxCol.ValueMember = "Tarrif" // when I add these rows the new Tarrif column is not visible
'---add a combobox column to the DataGridView control---
DataGridView1.Columns.Add(comboBoxCol)
End Sub
Private Sub Button_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim sqlCmdBuilder As New SqlCommandBuilder(dataadapter)
sqlCmdBuilder.GetUpdateCommand()
dataadapter.Update(ds.Tables("Customers_table"))
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
End Class
Hope that makes sense. Any help greatly appreciated.
A few things in this code:
dataadapter.Fill(ds, "Customers_table")
conn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers_table"
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Tarrifs"
'---data bind it---
comboBoxCol.DataSource = Tarrifs1
First, I'm not sure what happens whenever the code attempts to fill a DataSet that already has data in it. In my code, I never assume, so I always reinitialize it each time unless I need it:
ds = new DataSet()
Next, you are setting the DataSource for comboBoxCol as Tarrifs1, but I do not see that defined anywhere. You might want to look into that.
Finally, about the SQL: Dim sql As String = "SELECT * FROM Customers" I don't know what columns are in your Customers table. Is Tarrif an actual column?
I figured half my issue out.
I configured a second data connection to pull the Tarrif drop down from the Tarrifs table and display this as part of the customer data table.
My outstanding issues now, is that I need to write the select Tarrif value back to the Customers table.
All the other updated values save, just not sure how to write the Tarrif dropdown back to my SQL table
Here is my latest code.
Imports System.Data.SqlClient
Imports System.Data.Common
Public Class ViewCustomersForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=barry-laptop\SQLEXPRESS; database=BillingReferenceData; integrated security=yes"
Dim sql As String = "SELECT [Customer ID] ,[Customer Name] ,[Address] ,[City] ,[County] ,[Post Code] FROM [BillingReferenceData].[dbo].[Customers]"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim comm As SqlCommand = New SqlCommand(Sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
Dim con As New System.Data.SqlClient.SqlConnection("server=barry-laptop\SQLEXPRESS; database=Test; integrated security=yes")
Dim strSQL As String = "SELECT * FROM Tarrifs"
Dim da As New System.Data.SqlClient.SqlDataAdapter(strSQL, con)
Private Sub ViewCustomersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'---open the connection and fill the dataset---
conn.Open()
dataadapter.Fill(ds, "Customers_table")
conn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers_table"
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Tarrifs"
'---data bind it---
da.Fill(ds, "Tarrifs")
comboBoxCol.DataSource = ds.Tables("Tarrifs")
comboBoxCol.DisplayMember = "Tarrif"
comboBoxCol.ValueMember = "Tarrif"
'---add a combobox column to the DataGridView control---
DataGridView1.Columns.Add(comboBoxCol)
End Sub
Private Sub Button_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim sqlCmdBuilder As New SqlCommandBuilder(dataadapter)
sqlCmdBuilder.GetUpdateCommand()
dataadapter.Update(ds.Tables("Customers_table"))
dataadapter.Update(ds.Tables("Tarrifs"))
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
End Class