Firing an update off of a single change in a datagrid

E

Earl

I want to fire a database update off of a single change to a single cell in
the datagrid. This apparently cannot be done using keypress, keyup, keydown,
etc. I've read George Shepard's FAQ and while I may have overlooked it, I
have not found an answer. I now am using the CurrentCellChanged event, and
this is satisfactory IF the user moves off of the cell before closing the
form. An obvious alternative would be to check for changes before closing
the form, but I would rather do it at the moment the cell gets changed.
 
K

Ken Tucker [MVP]

Hi,

You can use the currency managers current changed event to detect
changes.

http://www.windowsformsdatagridhelp.com/default.aspx?ID=3e6c910e-3c58-4ce7-a626-9cc458453630

Dim ds As New DataSet
Dim WithEvents cm As CurrencyManager
Dim da As OleDbDataAdapter

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strConn As String
Dim strSQL As String
Dim conn As OleDbConnection
Dim cmd As OleDbCommandBuilder

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"
strConn &= "Data Source = Northwind.mdb;"

conn = New OleDbConnection(strConn)

da = New OleDbDataAdapter("Select * From Categories", conn)
da.Fill(ds, "Categories")
cmd = New OleDbCommandBuilder(da)

DataGrid1.DataSource = ds.Tables("Categories")

cm = CType(Me.BindingContext(DataGrid1.DataSource), CurrencyManager)
End Sub


Private Sub cm_CurrentChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cm.CurrentChanged
Dim dt As DataTable
Try
dt = ds.Tables("Categories").GetChanges()
If dt.Rows.Count > 0 Then
da.Update(ds, "Categories")
ds.AcceptChanges()
End If
Catch
End Try
End Sub

Ken
------------------
I want to fire a database update off of a single change to a single cell in
the datagrid. This apparently cannot be done using keypress, keyup, keydown,
etc. I've read George Shepard's FAQ and while I may have overlooked it, I
have not found an answer. I now am using the CurrentCellChanged event, and
this is satisfactory IF the user moves off of the cell before closing the
form. An obvious alternative would be to check for changes before closing
the form, but I would rather do it at the moment the cell gets changed.
 
E

Earl

Thanks Ken. I'm already using the currency manager (with a dataview) and
while it intercepts my changes WITHIN a cell, it does not catch the Delete
key with a row selected.
 
E

Earl

Sorry for what was somewhat of a confusing response ... basically where I'm
at right now is that the currency manager doesn't do any more than the
CurrentCellChanged event does. To trigger the currency manager, I would
still have to move out of the cell.

So I went past that, and decided to just use the OK/Close button to trigger
an update in case the user had NOT moved out of the cell. This of course
creates another issue -- what if the user had deleted a row -- with the
Delete key instead of the button? Well, I could call for a delete command
there, but then it appeared that I would have to derive a custom datagrid to
capture the delete key.

I'm tired, it's late, I've been working with this damn datagrid for going on
2 years now, and if everything has to be derived to provide basic
functionality, I'm not real sure it's worth the bother. I'm tired of working
out data relations in the grid too. I sure hope they get it right in 2005.
 

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