datagrid edit

G

Guest

Hi all

I am having a problem saving the contents of an edited datagrid back to the underlying SQL CE Database. I'm fairly new to .Net so I'm not even sure if this is possible.

I have a datagrid on my form which I have populated using a datatable

fstrTableName = strTableName
strSQL = "Select * From " & fstrTableName

Dim cmd As New SqlCeCommand(strSQL, ssceconn)

da = New SqlCeDataAdapter(cmd)
fsqlCB = New SqlCeCommandBuilder(da)
da.UpdateCommand = fsqlCB.GetUpdateCommand

table.TableName = fstrTableName
da.Fill(table)
DataGrid1.DataSource = table

This bit seems to work fine.

I then have a text box on my form which gets populated with the contents of each cell on the grid as you navigate around (copied from an example somewhere). If the user then updates the value in the text box, it is then updated automatically in the grid.

currentCell = DataGrid1.CurrentCell

table.Rows(currentCell.RowNumber).BeginEdit()
table.Rows(currentCell.RowNumber).Item(currentCell.ColumnNumber) = currentCellData

table.Rows(currentCell.RowNumber).EndEdit()
table.AcceptChanges()

Me.DataGrid1.Update()

This also seems to work fine.
However, when I close the form the changes are not being saved to the underlying database. I am doing a da.Update(table) in the closing form procedure, but this does not seem to make any difference.

Does anyone know whether what I am doing is possible, and if so, where I am going wrong.

Thanks
 
W

William Ryan eMVP

Glad you solved it. Just for an FYI, AcceptChanges does a few things like
actually deletes all the rows with a Rowstate of Deleted and sets the
rowstate of the other rows to unchanged. Calling it right before you call
update, all else being equal, is a guarantee that the changes you made prior
to that won't ever hit the db.

--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
Ian Parker said:
Hi all

I am having a problem saving the contents of an edited datagrid back to
the underlying SQL CE Database. I'm fairly new to .Net so I'm not even sure
if this is possible.
I have a datagrid on my form which I have populated using a datatable

fstrTableName = strTableName
strSQL = "Select * From " & fstrTableName

Dim cmd As New SqlCeCommand(strSQL, ssceconn)

da = New SqlCeDataAdapter(cmd)
fsqlCB = New SqlCeCommandBuilder(da)
da.UpdateCommand = fsqlCB.GetUpdateCommand

table.TableName = fstrTableName
da.Fill(table)
DataGrid1.DataSource = table

This bit seems to work fine.

I then have a text box on my form which gets populated with the contents
of each cell on the grid as you navigate around (copied from an example
somewhere). If the user then updates the value in the text box, it is then
updated automatically in the grid.
currentCell = DataGrid1.CurrentCell

table.Rows(currentCell.RowNumber).BeginEdit()
table.Rows(currentCell.RowNumber).Item(currentCell.ColumnNumber) = currentCellData

table.Rows(currentCell.RowNumber).EndEdit()
table.AcceptChanges()

Me.DataGrid1.Update()

This also seems to work fine.
However, when I close the form the changes are not being saved to the
underlying database. I am doing a da.Update(table) in the closing form
procedure, but this does not seem to make any difference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top