none
Datagridview dirty ????

    Question

  • When I leave a datagridview - how can I find out if any cell or row is changed by the user ?

    Best regards KSO, Denmark
    Tuesday, May 05, 2009 10:40 AM

Answers

  • Well, there are quite a few events in the DataGridView that you could try, like CellValidating, CellEndEdit, KeyDown, etc.

    Your best bet is to take a look at the available events and put in debug.WriteLine() statements in them so you can see when they occur.

    Good luck!
    www.insteptech.com
    • Marked as answer by PSAKSor Thursday, May 07, 2009 5:41 AM
    Thursday, May 07, 2009 12:31 AM

All replies

  • The datagridview won't keep track of this for you.

    Are you using binding? If you bind to a dataset/datatable it keeps track of changes.

    I bind to business objects and have a "IsDirty" flag in my base BO class that is updated whenever a property of the business object changes.

    Hope this helps.
    www.insteptech.com
    Tuesday, May 05, 2009 3:29 PM
  • I try tyo minimize the amount of code, so I use the "Drag&Drop"-method - a table from the Db drag&dropped on the dataset designer to get a datasource - this datasource I drag&drop to the form as a datagridview.

    I just want to know when the user edited anything in the datagridview - just pressed some keys in a cell WITHOUT moving from the field.

    Best regards KSO, Denmark
    Wednesday, May 06, 2009 6:08 AM
  • Well, there are quite a few events in the DataGridView that you could try, like CellValidating, CellEndEdit, KeyDown, etc.

    Your best bet is to take a look at the available events and put in debug.WriteLine() statements in them so you can see when they occur.

    Good luck!
    www.insteptech.com
    • Marked as answer by PSAKSor Thursday, May 07, 2009 5:41 AM
    Thursday, May 07, 2009 12:31 AM
  • Hi PSAKSor,

    Did you mean you want to capture the text changed event of the cell when the cell is keeping in focus?

    If so, you can first get the Editing control in the EditingControlShowing event of the DataGridView, and hook events for the Editting control. For example the editing control of the DataGridViewTextBoxCell is TextBox.

    You can write code as the following:

            void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                //Make sure the second column is textBox column.
                if (this.dgv.CurrentCell.ColumnIndex == 1)
                {
                    TextBox tb = e.Control as TextBox;
                    tb.TextChanged -= new EventHandler(tb_TextChanged);
                    tb.TextChanged += new EventHandler(tb_TextChanged);
                }
            }
    
            void tb_TextChanged(object sender, EventArgs e)
            {
                System.Diagnostics.Debug.WriteLine("text changed");
            }
    If that's off the base, please feel free to let me know.

    Best regards,
    Bruce Zhou
    Please mark the replies as answers if they help and unmark if they don't.
    Thursday, May 07, 2009 5:42 AM