Datagrid and currentcellchanged event

  • Thread starter Thread starter Suzanne
  • Start date Start date
S

Suzanne

Hi all,

I'm having problems with datagrids and the currentcellchanged event.
My problem is this: I have a datagrid on a form, if the user changes
the text in a cell on the datagrid then tries to close the form via a
button on the toolbar (with going to another cell in the datagrid)I
want to be able to popup a messagebox to the user asking them if they
want to keep their changes.

At the momment I'm accomplishing this by a property that I set to true
in the currentcellchanged event. However if the user just tabs around
the datagrid this event is triggered even though though from the
user's point of view they haven't changed any text.

I still want the user to be able to tab around the datagrid but
perhaps not have the tab key trigger the currentcellchanged event. Is
this possible?

Another way I have thought of doing this is by trying to get the
current datagrid cell's value (using datagrid.text) and then comparing
it to the underlaying datasource's value for that cell (& if the 2 are
different then setting my property to true). However I can't find a
way of getting hold of the datagrid's datasource's value for the
current datagrid cell.

Any ideas would be most gratefully received - one more thing - my
datagrid is a custom control and I would like the above functionality
to be included within the custom control rather than the form that it
sits on.

Thanks in advance for any help
Suzanne
 
Suzanne,
I still want the user to be able to tab around the datagrid but
perhaps not have the tab key trigger the currentcellchanged event. Is
this possible?

You cannot do that, well, unless you don't inherit from DataGrid and tap
into its event handling logic. And I'd say this is not the best way to do
so.
Another way I have thought of doing this is by trying to get the
current datagrid cell's value (using datagrid.text) and then comparing
it to the underlaying datasource's value for that cell (& if the 2 are
different then setting my property to true). However I can't find a
way of getting hold of the datagrid's datasource's value for the
current datagrid cell.

I like this approach much better. And it is relatively simple to get hold of
the corresponding source data row:

CurrencyManager cm = this.BindingContext[dataGrid.DataSource,
dataGrid1.DataMember];
DataView view = (DataView)cm.List;
DataRow theRow = view[dataGrid1.CurrentCell.RowNumber /* -1 probably
*/].Row;

You can then analyze theRow's RowState property to see if it has been
changed.

P.S. I beleive this code can easily be modified to become a part of a custom
DataGrid-based control.
 
Can't you use the datarowversion.original from each cell?
Or do I misunderstand the question?
oldvalue= tmprow("fieldname", DataRowVersion.Original)

newvalue=tmprow("fieldname",datarowversion.modified)

tmprow is a row in the table where you putted the changes in

Dim updatedrows As DataTable =
dataset1.Tables(0).GetChanges(DataRowState.Modified)

Tupolev
 
Hi Suzanne,
If you want to know which cell is being changed, I agree with Mr. Tupolev.
You can take advantage of the versioning mechanism.
However, if you don't care which cell is being changed, maybe you needn't
handle the OnCurrentCellChanged event, you may try the HasChanges method of
the DataSet and GetChanges method of the DataTable. For more information
you may refer to
<DataSet.HasChanges>
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemdatadatasetcla
sshaschangestopic.asp?frame=true
<DataSet.GetChanges>
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemdatadatasetcla
ssgetchangestopic.asp?frame=true
<DataTable.GetChanges>
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDataDataTableC
lassGetChangesTopic.asp?frame=true

If you still have problems please let me know, thanks!


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" shouldbe removed before
sending, Thanks!

--------------------
| From: (e-mail address removed) (Suzanne)
| Newsgroups:
microsoft.public.dotnet.languages.vb,microsoft.public.dotnet.framework.windo
wsforms,microsoft.public.vb.controls.databound
| Subject: Datagrid and currentcellchanged event
| Date: 22 Sep 2003 02:54:57 -0700
| Organization: http://groups.google.com/
| Lines: 32
| Message-ID: <[email protected]>
| NNTP-Posting-Host: 213.122.87.1
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1064224498 14543 127.0.0.1 (22 Sep 2003
09:54:58 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: 22 Sep 2003 09:54:58 GMT
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-
xit-09!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:52846
microsoft.public.vb.controls.databound:21635
microsoft.public.dotnet.languages.vb:140184
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Hi all,
|
| I'm having problems with datagrids and the currentcellchanged event.
| My problem is this: I have a datagrid on a form, if the user changes
| the text in a cell on the datagrid then tries to close the form via a
| button on the toolbar (with going to another cell in the datagrid)I
| want to be able to popup a messagebox to the user asking them if they
| want to keep their changes.
|
| At the momment I'm accomplishing this by a property that I set to true
| in the currentcellchanged event. However if the user just tabs around
| the datagrid this event is triggered even though though from the
| user's point of view they haven't changed any text.
|
| I still want the user to be able to tab around the datagrid but
| perhaps not have the tab key trigger the currentcellchanged event. Is
| this possible?
|
| Another way I have thought of doing this is by trying to get the
| current datagrid cell's value (using datagrid.text) and then comparing
| it to the underlaying datasource's value for that cell (& if the 2 are
| different then setting my property to true). However I can't find a
| way of getting hold of the datagrid's datasource's value for the
| current datagrid cell.
|
| Any ideas would be most gratefully received - one more thing - my
| datagrid is a custom control and I would like the above functionality
| to be included within the custom control rather than the form that it
| sits on.
|
| Thanks in advance for any help
| Suzanne
|
 
Thanks for all the suggestions, unfortunately it's still not behaving
exactly as I'd planned...

I'm unable to use the versioning mechanism Mr. Tupolev suggests as
when the user clicks on the toolbar (with the focus still being in the
edited cell of the datagrid) I don't think that the underlying
datatable has yet been updated with any changes? Which is really the
whole problem!

Dmitriy's suggestions for using the currency manager to get the
corresponding source data row were alot closer to what I was looking
for (and does return this value) - however I was planning to check the
value of underlaying datasource's value for that cell against the new
text of the datagrid cell when the currentcellchanged event was
triggered.

The problem is that this event is trigged as soon as the user tabs
into the cell and then I guess for every keystroke afterwards? So I
don't know when my user has finshed editing the cell (if it is the
custom control datagrid that is handling this functionality rather
than the form it sits on).

The solution that I'm using - which works - but is not pretty - is
that in the code for my form, when the user clicks on the toolbar I
run a bit of code that sets the currentcell of my datagrid to the 1st
cell in the grid (if the user is not currently editing the first cell,
if they are then I set the current cell to be another cell). Basically
I just force the datagrid to update its datasource by moving out of
the edited cell, so I don't need to use the currentcellchanged event
at all.

If any one can think of a more elegant way to do this then I'd be most
interested

Again thanks for all the help

Suzanne
 
Hi Susanne,

Have you tried using the aftercoledit event. It might do what you want. If
nothing has been changed, the event won't fire.

Suzette
 
Back
Top