Visual indicator problem when record is changed

Posted on 2006-06-03
Medium Priority
Last Modified: 2012-06-27
Hi all

I have a form with a datagridview and a couple of fields on it. These are bound to a dataset.
When I select a record in the datagridview, the correct information shows up in the fields.

Now I want to give the user a visual indicator that data on the form has changed.
To do this I have put a panel at the top of the form. The idea is :
if the data has not changed the panel is green
If the data has been changed by the user, the panel goes red.

This is causing problems.
I had the event to change the colour in the textchange event. Problem: when i move to the next record, the text changes and I get a red flag when the user has not changed data.
I can work around this using the keypress event for text fields, the click event for checkboxes as these are not triggered when the text for the next record is displayed.
What do I do with the dropdown boxes to get the same effect?

Also just noticed : If I use the mouse and select and cut the data from the text field, the keydown event (obviously) is not triggered.

Rowchange OR rowchanging does not get triggered until I select the next record in the datagridview

Can anyone suggest a simple way to implement what I thought would be basic functionality?

I realy would like my users to know when a single field has been changed as they change it

Question by:tcss
  • 5
  • 4
LVL 11

Expert Comment

ID: 16824740
This does not answer your question, but what I have done is to display all of the data in readonly form, and then force the user to specifically indicate that they want to change data by pressing an edit button. At that point I go through all of the controls and make them editable.
Then, I don't let them naviagate until they press accept or discard buttons.

This is not a cool as what you are rying to do, but it is a lot less work.

LVL 12

Expert Comment

ID: 16824787
You can do that nice in ColumnChanging event of the Datatable that represents the source for datagridview.
After every position change in the datagridview make the panel green (see PositionChanged event of currencymanager or bindingsource objects)

Author Comment

ID: 16826009
Thanks for the comments

A little bit of an explanation as to why I am trying to do what I am doing.

There can be a lot of reecords that need to be edited (ie employee list 300 - 3000 ) where I want to keep the users keystroke/mouse input to a minimum while editing the records.

I want the user to be able to move to the next record without being prompted to save the record. At the same time the user must notice if the record has been changed and before moving to the next record have the option to cancel the changes.

I will try and implement the column changing event option. I suspect that the notification will be a little too late if the user changes a record and then clicks on the next row in the grid. The complication here is that you do not want to prompt the user to save every record. To do that the indicator needs to be set before you move to the next record.

Will report back here.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 12

Expert Comment

ID: 16826129
ColumnChanging event is fired when you leave the control (textbox, combobox, grid cell etc) bound to table field. A little weird behavior will appear when you change a single field in a record and then click directly on 'next' button. The panel will go red and immediately green. And what if, after a 'next' click you click on 'previous' button. You are on a previously modified record and the panel should be red, isn't it? That means that in a positionchanged event you need to check RowState property of the current row and color the panel accordingly.

Author Comment

ID: 16826946
As suggested. Only when the control is exited does the event fire.

The idea is to commit the changes when the user moves to the next record. So when you move to a previously modified record, the panel is again green as all changes have been committed.

Refer to original question. How can I stop the events being fired while I am loading the grid or changing records? Does .net not have such a feature in it?

LVL 12

Expert Comment

ID: 16831472
There is no general way to stop all the events. There are stil some methods that temporarily suspends datatable or datarows events. You can use BeginEdit method of a DataRow to allow multiple changes to be made without validation. For DataTable see BeginLoadData/EndLoadData methods.  You can also set EnforceConstraints property of a dataset to false/true.

   Otherwise you only have the options to manually detach the handlers when you don't want them to be called, or
        use global variables that are verified inside event handlers before performing specific operations.

Author Comment

ID: 16832651
Points raised because this question is drifting a bit and going on for a while.

So what causes haschanges to return true?

If I change the value on a column and move to the next column I get the column changed event. It appears that you only get the column changed event if you change data in the column and then move to another column, not if you change to another column without changinbg data. This is usefull.
If I check the haschanges value at this stage it is still false.
e.Row.Rowstate is also unchanged.

If I move to the nest row and then check haschanges it is true.
e.Row.Rowstate is now modified too.

It would be usefull if row.RowState could be set when the column changed and not when the row changes (same for haschanges).

What am I missing here. Is there a way to know from interigating a flag or something like that on the datarow or dataset?

LVL 12

Accepted Solution

sumix earned 2000 total points
ID: 16835412
ColumnChanging and ColumnChanged events are fired when the VALUE of a field is modified not when you navigate, as you noticed.
In a datagrid, when you change the value in a cell, BeginEdit is called, but the values aren't committed in the table until you move to another row or leave the grid. If you need, you may call EndEdit method of the current row in ColumnChanged event handler, this will change the RowState.

One reason for not committing the changes immediatly is that you can have constraints that implies more than one field (like filed1 = filed2+filed3) and these shouldn't be checked until you modify the entire row.

Author Comment

ID: 16835958
No constraints as far as I am aware of that nature. there may be foreign keys involved though.

I am not editing in the grid. My form is split into two halves. The grid on the left and fields on the right.
Both are bound to the same dataset. Can I assume that when I start editing the the data in the field begin edit is called too. When I call Dataset.Update, the changes reflect in the database.

I am going to (for now) have to use the column change event as a way of deciding if the user changed the record and show the red panel because this handles my combo boxes too.
There are still a few issues that I am trying to come to terms wiuth (this is all new to me) such as calling update from within a rowchanged event. It does not seam to like it that much.
It would only be fait to say that they fall outside the scope of the original question. Next interisting step is to handle inserting new records. That too will be for a later question.

Thank you for the time invested in reading and commenting on my ramblings. It has been a good learning experiance for me.
LVL 12

Expert Comment

ID: 16839955
When you start editing a value in a bound control (not in a datagridview), current row enters in edit mode, but the value is committed in the row after you leave the control and row state should be now 'Modified'.

Second, in my opinion, Update should be called when you finished editing the current row (and move to another row - this is PositionChanged, or close the form), not in RowChanged event.


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question