Forcing a DataGridView to update values

I've got a datagridview (winforms) controls with a collection bound to it.  Anyway, I've got a checkbox column in the grid.  I need its value to be updated to the object as soon when the column value is changed.  However, it only gets updated when I move focus off of that cell.  Is there anyway to change the way it behaves or call a method to force the grid to update values back to the bound collection   For instance, on a regular databinding to like a textbox I can change the DataSourceUpdateMode to OnPropertyChange.  Is there something similar to this for the entire DataGridView


Answer this question

Forcing a DataGridView to update values

  • Bernd VanSkiver

    Its kinda like a normal button -- it has a click event that occurs when you use the space bar or enter key or use the mouse. We have Mouse** events as well to know when the user uses the mouse to perform an action.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    -mark

    Program Manager

    Microsoft

    This post is provided “as-is”

     


  • akqajohn

    You can use the DataGridView CellContentClick event to find out when the user clicked on the check box or when the user changed the check by hitting the space bar.

    At that point you can execute DataGridView::EndEdit to commit the cell column. And then call BindingSource::EndEdit to commit the entire row.



  • Pl_john

    I just wanted to prevent the user from checking the CheckBox if a condition was met. My DataGridViewCheckBoxCell had an underlying column that was boolean. To get this to work I had to perform the following in CellContentClicked:

    if (_remainingBalance == 0)
    {
    grdPayments.EndEdit();
    _paymentsBindingSource.EndEdit();
    grdPayments.BeginEdit(false);
    grdPayments.Rows[e.RowIndex].Cells["Assign"].Value = false;
    _dtPayments.Rows[e.RowIndex]["Assign"] = false;
    grdPayments.EndEdit();
    _paymentsBindingSource.EndEdit();
    grdPayments.Update();
    return;
    }



  • DannySmurf

    The problem is that there's no way of knowing when the data in the cell has changed without moving the focus away from the cell. For example, what if a user starts typing text in the cell, then pauses, and then comes back to enter more text. Should an event have been raised during the pause No.

    In the DataGrid control, each cell is associated with a TextBox control. You can use the TextBox control's TextChanged event to monitor real-time text changes. That still doesn't tell you, however, when a user is "finished" editing the contents of a cell.



  • BhavikS

    Ahh, yes, I use to have this problem too.

    Once you have determined that the user is "done" checking or unchecking the CheckBox, call the EndEdit method of the underlying BindingSource object or the EndCurrentEdit of the underlying BindingContext:

    BindingSource:

    this.bindingSource1.EndEdit();

    BindingContext:

    this.BindingContext[this.dataGrid1.DataSource].EndCurrentEdit();

    This will cause any changes in the control (DataGrid, DataGridView, etc.) to commit to the underlying data source (DataSet, DataTable, etc.).



  • bhakes

    Thanks for showing your code. Sometimes I think these alternative ways of writing code are meant to save time. what again is so great about the BindingSource
    I guess it saves you from having to type Sql update statements but if you are used to doing that, you will not save much more time writing same code:

    UPDATE Payments
    SET Assign = false
    WHERE PaymentKey =

    Is it correct that you juggle writing SQL statements versus the various Update, EndEdit, etc. methods I just want to understand microsofts objective

  • devalapa_k

    I see, so then it's just the name of the event that's misleading: CellContentClick - indicating it's associated with only a mouse event.

  • FiftyFeet

    CellContentClick fires when you click either with the mouse or use the keyboard to activate the checkbox.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    -mark

    DataGridView Program Manager

    Microsoft

    This post is provided “as-is”

     


  • TnTico

    My best guess for this is to build your own custom datagridview column of the combobox. Then bind to the combobox's selectedvaluechanged event and raise a cellvaluechanged event inside of the column. I know this isn't the answer you wanted, but its a start.



  • Don Peterson

    Maybe I just don't "get it", but why can't you just expose the events of the underlying controls Why can't I access the "CheckChanged" event or the "Checked" property

    This stupid checkbox has caused me HOURS of grief making it behave like it would if I were using Infragistics (which I loathe).

    I know your controls may be elegantly crafted for the object-oriented world, but for individuals like myself just trying to get the job done, you make it extremely difficult.

  • nrs251

    The only problem with that is Accessibility: a user may not use a mouse to place focus on a given cell - the keyboard could be used. You can tab through the cells and then press the space bar to check or uncheck a CheckBox (although unlikely).

  • ArtusKG

    Yes. This does work. There is one minor issue when using the keyboard (spacebar) to change the check. For some reason, I must move off the cell and back on if I want to change the value a second time. This was corrected by adding .beginedit(true) in the cellContentClick event after the .endedit().



  • blahmoo64

    I'm having the same issue but with a combo box. Cellcontentclick event won't be fired when clicking the combo. What should I do

    Thanks.


  • leandro_

    I understand the problem on a textbox column.  But I'm having the trouble with a checkbox column.  When someone changes the check, the value needs to be updated in the datasource.  Any ideas

  • Forcing a DataGridView to update values