Visual indicator problem when record is changed

Posted on 2006-06-03
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
    LVL 11

    Expert Comment

    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

    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)
    LVL 1

    Author Comment

    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.
    LVL 12

    Expert Comment

    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.
    LVL 1

    Author Comment

    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

    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.
    LVL 1

    Author Comment

    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

    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.
    LVL 1

    Author Comment

    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

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now