force datagrid save?

  • Thread starter Jonathan Crawford
  • Start date
J

Jonathan Crawford

Hi

I have a datgrid and when I click the toolbar or menu
which has a save action, the last change is ignored.
Is there anyway to force the datagrid to save
the current record?

thanks

jonathyan

--

===============
Jonathan Crawford
01273 440018
07799 068570
fax 01273 380221
(e-mail address removed)
===============
 
T

TT \(Tom Tempelaere\)

Hi Jonathan,

Jonathan Crawford said:
Hi

I have a datgrid and when I click the toolbar or menu
which has a save action, the last change is ignored.
Is there anyway to force the datagrid to save
the current record?

thanks
jonathyan

Assuming that your data source is a DataTable (DataSource property of your
grid), call AcceptChanges on the DataTable. I haven't done this before, tell
me if this works.

Hope this helps,
Tom T.
 
J

Jonathan Crawford

Hi

On the menu click I did

myDataTable.accept changes
but that did no good. I have some validation code which check
for null values and it failed to pick up the current line


I have tried currencymanager
and
databindingmanager.endcurrentedit
and
mydatagrid.endedit(nothing, mydatagrid.currentrowindex,nothing)

but have had zero luck

I have 4 fields, three of them are combo boxes and just want to ensure
none are empty when the xml file is saved, but to no avail.

If I transfer it to a command button it is fine, however the
whole design is based on menus.

thanks

Jonathan
 
T

TT \(Tom Tempelaere\)

Jonathan,

Jonathan Crawford said:
Hi

On the menu click I did

myDataTable.accept changes
but that did no good. I have some validation code which check
for null values and it failed to pick up the current line


I have tried currencymanager
and
databindingmanager.endcurrentedit
and
mydatagrid.endedit(nothing, mydatagrid.currentrowindex,nothing)

but have had zero luck

I have 4 fields, three of them are combo boxes and just want to ensure
none are empty when the xml file is saved, but to no avail.

If I transfer it to a command button it is fine, however the
whole design is based on menus.

thanks
Jonathan

Have you tried ending the edit first, and then calling AcceptChanges?

I wonder why the command button works. I don't really know how the app is
set up. What is so different about way you do things for the button and the
menu?

Kind regards,
Tom T.
 
G

Guest

First, I'm new to this group and this is my first reply.

Second, I would like to know a better solution to this. I have a solution
that I've used already, but its awkward.

I think what is happening is this:
When you edit cells in a row, they are buffered in the grid and don't get
saved to the DataRow object until the user navigates to another row (using
down arrow, up arrow, clicks on another row etc.). If the user navigates to
a cell on the same row, the data just stays sitting in the grid.

So in the scenario where the user edits a cell and then selects a menu
command, the data isn't in the DataRow yet. So AcceptChanges isn't going to
do anything for us.

We can't rely on the user to move to another row on their last grid edit, so
how do we flush the data from the grid to the DataRow within code? I've used
EndEdit to finish the cell, and then modified the CurrentCell property to
alter the current row up or down one from the current row.

This is a bit awkward. There should be a simple grid call to force the data
to be flushed. In particular, how would I handle a one row display in the
grid (which could happen when you don't have an "add row" at the bottom due
to disabling of new rows).

Any better solutions would be appreciated.
 
G

Guest

Brian,
You have hit on one of the conundrums of design when allowing usrs to update
data base records. Different programs over the years have handled this
problem in different ways. In general, the convention seems to be that if
user makes a change and moves to a different row in the table (without
abandoning the change) then the change should be glued down without further
action required by the user. That is user makes change, moves to new row,
and changes in previous row are saved.

The design problem has always been what if the user does not explicitly move
to another row but uses a menu or something else to pull focus away from the
editing process? (I'm being vague here because this really applies to both
the data grid view of the world and the classic form view of the world i.e.
MS Access Tables and Forms.) Different designers answer this question in
different ways. From what you have said, the datagrid abandons the changes
when focus leaves it but before the row has changed. This is entirely
consistent with the first do no harm school of thought on this subject. It
requires the user to "accept" his changes by always moving to another row.
Different strokes for different folks.

I'm running into a different problem. The user can't type directly into the
grid. He uses a contextmenu to pick a date - yesterday, today, or the date in
a date time picker. The problem is two fold
1) 1f I don't do anything, then the value entered by the program is not
displayed until the user moves out of the cell. I want it displayed without
accepting it until the user moves to another row.
2) If I force the display by moving down and back up a row, everything is OK
UNLESS the user has sorted on the field that is being changed. You don't end
up back where you started under these circumstancces because the new value
will typically change the sort order UGH

More when I resolve these issues.
--
Regards,
Al Christoph
Senior Consultant and Proprietor
Three Bears Software, LLC
just right software @ just right prices @3bears.biz
 
G

Guest

For our user base, I don't think they will appreciate the "movement" to
another row requirement. From their point of view, if they change a cell, it
should "take". For example, if they change several rows, do a "Save" and
then come back and requery their data later, they will be disappointed if the
last row they edited didn't save.

I implemented a pick list chooser (a combo box) for a grid cell, and had
similar issues to your context menu issue. For your two problems:
1) I presume you are using a DataGridTableStyle with DataGridColumnStyles.
You should create a derived class from DataGridColumnStyles to use for your
date column. Then you override the Commit method and use the
SetColumnValueAtRow method to update the grid. I believe this technique
updates the grid, but not the underlying row until the user changes rows.
2) With sorting enabled, I found that I couldn't track the current row by
row number for the reason you stated. I need to track the row so I could do
some inter-field validation after the row was finished being edited
(ironically, when the user changes row). I found that I needed to track the
current row by maintaining a reference to the Row object.
 
G

Guest

Thanks Brian for the clues to doing this right. By doing it right in this
context I mean something other than changing row currentcy to force the value
to stay glued down.

Here's what I ended up doing FWIW:

Public Class dgcs
Inherits DataGridTextBoxColumn
Dim mNewValue As Object
Dim isEditing As Boolean = False
Property newValue() As Object
Get
Return mNewValue
End Get
Set(ByVal Value As Object)
mNewValue = Value
If Me.DataGridTableStyle.DataGrid.Parent Is Nothing Then
Exit Property
Me.isEditing = True
Dim dgt As DataGrid = Me.DataGridTableStyle.DataGrid
Dim r As Integer = dgt.CurrentCell.RowNumber
Dim cm As CurrencyManager = CType(dgt.Parent,
Form).BindingContext(dgt.DataSource)
Me.Commit(cm, r)
End Set
End Property
Protected Overrides Function Commit(ByVal dataSource As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean
If Me.isEditing Then
Me.isEditing = False
SetColumnValueAtRow(dataSource, rowNum, Me.mNewValue)
Me.HideEditBox() ' THIS IS CRUCIAL
Me.Invalidate()
End If
Commit = True
End Function
End Class

......

Private Sub putDate(ByVal d As Date)
Dim curDgcs As dgcs =
Me.DataGrid1.TableStyles(0).GridColumnStyles(Me.DataGrid1.CurrentCell.ColumnNumber)
curDgcs.newValue = d
End Sub
Private Sub miYesterday_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles miYesterday.Click
Me.putDate(Today.AddDays(-1))
End Sub
Private Sub miToday_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles miToday.Click
Me.putDate(Today)
End Sub
--

Things I didn't show - the conversion of the relevant column styles from the
standard to my class. I did a no no and dis this in the windows generated
code.

The user right clicks in a cell, picks yesterday or today. The click event
causes the right newvalue to be set and the commit makes sure that it is
displayed.

By calling commit from the newValue setting property I get the kind of
automatic behavior we've been looking for here.

WHAT IS EVEN MORE MAGICAL is that for whatever reason if i have the column
as the sort, things reorder correctly without further intervention on my
part!!!!!

I'm going to modify this a bit further so that the sql gets fired off to
update the underlying tables. (This is NOT trivial in my particular case for
a variety of reasons:)))

Now, if I could only figure out how to get the (*&^(*&^ EM_SETTABSTOPS to
work. (If you have a clue search for my posting of the other day on the
subject in these forums.)

Regards,
Al Christoph
Senior Consultant and Proprietor
Three Bears Software, LLC
just right software @ just right prices @3bears.biz
 

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