DataGridView EndEdit/AcceptChanges

Posted on 2009-04-24
Last Modified: 2012-06-21
I have a DataGridView and when the user checks a checkbox in a row and closes the form immediately as the next click, I would like to display a message to the user saying 'changes not saved'.

When I call underlying DataTable.GetChanges(), it's always returning null. So on the form close I'm calling DataGridView.EndEdit() method. After this call, I see the datatable is updated with the last change by the user, but DataTable.GetChanges() is still returing null. Any ideas on this?
Question by:praveenuni
    LVL 15

    Expert Comment

    When is the Update query being run? Is it in your DataTable.RowChanged event?
    It's quite possible that when datagridview is losing focus (when you click the form's close button), the changes are being sent to the database. If you're using an SqlDataAdapter to execute these changes then it will set the RowState to Unchanged for all the rows.
    Calling GetChanges on the DataTable will return a null reference as there are no changes to be done.

    Author Comment

    There is no update or DataAdapter. I'm just using a plain dataset with some test data( with no connection to database). I found a link, which shows this as a bug

    I'm wondering if there is a fix for this?
    LVL 15

    Accepted Solution

    Strange, I can't replicate this problem. I've added a DataGridView to the form.
    Hook the FormClosing event.
    In the constructor create as basic table that has a boolean column, populate with some random data. Accept changes on the DataTable (and verify GetChanges() returns null)
    Set the DataTable as the datasource for the DataGridView.
    Change the checkstate of a row, and close the form.
    In FormClosing,AcceptChanges() on the DataGridView, GetChanges() returns a table with a single row.

    Are you doing anything wildly different from the above?
    Can you confirm the contents of the datatable after you call AcceptChanges (rather than using GetChanges())?

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    746 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

    18 Experts available now in Live!

    Get 1:1 Help Now