Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update database from DataGridView

Status
Not open for further replies.

wrzek

Programmer
Aug 29, 2005
12
PL
Hello,

I have VB 2005 Express Edition and i'm trying to understand the DataGridView control...

I have DataGridView "GrdSpec" connected to my Access db:

DaTbSpec = New OleDb.OleDbDataAdapter("Select *
from tbSpec", ConnQual)
DaTbSpec.Fill(DsTbSpec, "TbSpec")

Dim BsTbSpec As New BindingSource
BsTbSpec.DataSource = DsTbSpec.Tables(0)
GrdSpec.DataSource = BsTbSpec

and function:

Private Sub Save()
Dim myBuilder As OleDb.OleDbCommandBuilder
= New OleDb.OleDbCommandBuilder(DaTbSpec)
DaTbSpec.Update(CType(BsTbSpec.DataSource, DataTable))
End Sub

Function "Save" works fine but only when I manually change the values in my DataGridView.

But when I use something like:
GrdSpec.Rows(0).Cells(9).Value = "new value"
My Save Function doesn't work. New value is visible in grid, but the database is not changed. Anybody knows why?

Thanks in advance for any idea!
Larry
 
When you update the data in a datagridview it implicitly calls the BeginEdit method on the datarow that underlies the datagridview row being edited. What this means is that the changes are not "permanent" in the datarow until you call EndEdit, whether explicitly in your code or until you click off of the datagirview row being edited (such as by clicking on the Save button).

What this all means is that you need to call EndEdit on the rows you change programmatically, before you try to save the changes to the database.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Thanks a lot for help!

GrdSpec.Rows(0).Cells(9).Value = "New value"
BsTbSpec.EndEdit()
Dim myBuilder As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(DaTbSpec)
DaTbSpec.Update(CType(BsTbSpec.DataSource, DataTable))

Works great!
Larry

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top