jclothier
asked on
Basic DataGrid Example
HI
I have just discovered the DataGrid control and I could do with some help.
I need the complete code that does the following:
Retrieves the data from one simple table in an Access database and displays it in the datagrid. I then want to be able to edit an item in the grid or add a new row or two. I then want to click on a Save button and these changes to be saved back to the Access database.
500 points and an A rating for the person that can supply the complete code for this that I can get working.
Thanks in advance
I have just discovered the DataGrid control and I could do with some help.
I need the complete code that does the following:
Retrieves the data from one simple table in an Access database and displays it in the datagrid. I then want to be able to edit an item in the grid or add a new row or two. I then want to click on a Save button and these changes to be saved back to the Access database.
500 points and an A rating for the person that can supply the complete code for this that I can get working.
Thanks in advance
ASKER
That has certainly populated the grid but when I exit the changes are not saved
Save changes:
adapter.Update(table)
Bob
adapter.Update(table)
Bob
ASKER
nope that doesn't seem to work. I put adapter.Update(table) in the click event for my save button and nothing happened.
1) adapter will need to be declared with scope that covers both the .Fill and the .Update.
2) Somewhere after .Fill but before .Update you will need
Dim cb As New OleDbCommandBuilder(adapte r)
3) Make sure that, when you've edited one row in the datagrid, its changes get back to the datatable by clicking on a different row before trying the .Update.
Roger
2) Somewhere after .Fill but before .Update you will need
Dim cb As New OleDbCommandBuilder(adapte
3) Make sure that, when you've edited one row in the datagrid, its changes get back to the datatable by clicking on a different row before trying the .Update.
Roger
And I should have added that the commandbuilder will only work properly if the table has a PrimaryKey
Roger
Roger
You can try the following....make sure that the DataSet is in overall scope.
========================== ========== ========== ==
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
' create a connection string
Dim connString As String = "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source=Northwind.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection
' create a new dataset
Dim ds As DataSet = New DataSet
' The form load will populate the datagrid with data from Access table
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim da As New OleDbDataAdapter
Dim selectCmd As New OleDbCommand
myConnection.ConnectionStr ing = connString
' define the select command
With selectCmd
.Connection = myConnection
.CommandText = "Select * From Customers"
.CommandType = CommandType.Text
End With
' fill dataset
With da
.SelectCommand = selectCmd
.Fill(ds, "Customers")
End With
' display in the grid
DataGrid1.DataSource = ds.Tables("Customers")
End Sub
' the button1 click will take the grid information and post back to the database
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim da As New OleDbDataAdapter
Dim updCmd As New OleDbCommand
' create update command - example here only updates the company name
With updCmd
.Connection = myConnection
.CommandText = "Update Customers Set CompanyName = @CompanyName Where CustomerID = @CustomerID"
.CommandType = CommandType.Text
.Parameters.Add(New OleDbParameter("@CompanyNa me", OleDbType.Char, 40, "CompanyName"))
.Parameters.Add(New OleDbParameter("@CustomerI D", OleDbType.Char, 5, ParameterDirection.Input, False, CType(0, System.Byte), CType(0, System.Byte), "CustomerID", DataRowVersion.Original, Nothing))
End With
' need to now do an insert command and a delete command if you are adding and deleting data
' update the database
With da
.UpdateCommand = updCmd ' specifiy what update command to use
.Update(ds, "Customers")
End With
Me.Close
End Sub
========================== ========== =======
HTH :)
==========================
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
' create a connection string
Dim connString As String = "Provider=Microsoft.Jet.OL
Dim myConnection As OleDbConnection = New OleDbConnection
' create a new dataset
Dim ds As DataSet = New DataSet
' The form load will populate the datagrid with data from Access table
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim da As New OleDbDataAdapter
Dim selectCmd As New OleDbCommand
myConnection.ConnectionStr
' define the select command
With selectCmd
.Connection = myConnection
.CommandText = "Select * From Customers"
.CommandType = CommandType.Text
End With
' fill dataset
With da
.SelectCommand = selectCmd
.Fill(ds, "Customers")
End With
' display in the grid
DataGrid1.DataSource = ds.Tables("Customers")
End Sub
' the button1 click will take the grid information and post back to the database
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim da As New OleDbDataAdapter
Dim updCmd As New OleDbCommand
' create update command - example here only updates the company name
With updCmd
.Connection = myConnection
.CommandText = "Update Customers Set CompanyName = @CompanyName Where CustomerID = @CustomerID"
.CommandType = CommandType.Text
.Parameters.Add(New OleDbParameter("@CompanyNa
.Parameters.Add(New OleDbParameter("@CustomerI
End With
' need to now do an insert command and a delete command if you are adding and deleting data
' update the database
With da
.UpdateCommand = updCmd ' specifiy what update command to use
.Update(ds, "Customers")
End With
Me.Close
End Sub
==========================
HTH :)
ASKER
Thanks cubixSoftware
That seems to work quite nicely. Can you provide me with the code for adding a new row (by the way my ACcess database uses Autonumber for its primary key field) and then I will avoid you the points.
That seems to work quite nicely. Can you provide me with the code for adding a new row (by the way my ACcess database uses Autonumber for its primary key field) and then I will avoid you the points.
ASKER
Sorry meant to say award not avoid ;-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Absolutely Perfect! Thanks CubixSoftware
Dim adapter As New OleDbDataAdapter("<command
adapter.Fill(table)
Me.DataGrid1.DataSource = table
Bob