Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Forcing a DataGridView to update values
 

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?
JonM  Friday, December 16, 2005 5:50 PM

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.

Daniel Herling - Student  Friday, December 16, 2005 10:58 PM

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.

DotNetFun  Friday, December 16, 2005 6:10 PM
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?
JonM  Friday, December 16, 2005 7:08 PM

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.).

DotNetFun  Friday, December 16, 2005 7:31 PM

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.

Daniel Herling - Student  Friday, December 16, 2005 10:58 PM
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).
DotNetFun  Friday, December 16, 2005 11:26 PM

CellContentClick fires when you click either with the mouse or use the keyboard to activate the checkbox.

 

-mark

DataGridView Program Manager

Microsoft

This post is provided “as-is�o:p>

 

Mark Rideout  Saturday, December 17, 2005 1:26 AM
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.
DotNetFun  Saturday, December 17, 2005 6:16 AM

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.

 

-mark

Program Manager

Microsoft

This post is provided “as-is�o:p>

 

Mark Rideout  Saturday, December 17, 2005 5:24 PM

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.

LearnToRock  Friday, July 21, 2006 6:52 PM

Yes. This does work. There is one minorissue 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().

JonM  Tuesday, August 01, 2006 7:48 PM

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.

JonM  Tuesday, August 01, 2006 7:51 PM
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.
stu1234  Thursday, November 30, 2006 5:04 PM
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;
}


stu1234  Thursday, November 30, 2006 5:16 PM
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
Eric66  Tuesday, December 12, 2006 11:36 PM
After messing with this, I realized performing updates in the CellContentClick event is wrong because the value of checkbox is the opposite of its value the user intends.
The reason this is a problem for me is because I need to know what the value is after the user is finished clicking, not before.
According to msdn, the event to code is CellValueChanged.
http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.cellcontentclick.aspx
Eric66  Wednesday, December 13, 2006 12:00 AM

There's an easy way to get DataGridViewCheckBoxCell and DataGridViewComboBoxCell cells to commit changes without having to move focus to another cell. I found out how to do it by reading the class introduction for DataGridViewCheckBoxCell in MSDN. Capture the "CurrentCellDirtyStateChanged" event for the DataGridView and inside the handler:

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)

{

if (dataGridView1.CurrentCell is System.Windows.Forms.DataGridViewCheckBoxCell ||

dataGridView1.CurrentCell is System.Windows.Forms.DataGridViewComboBoxCell)

{

dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

}

}

This generates the "CellValueChanged" event on the grid and will also update any items bound to the check box and combo box cells.

It worked great for me. I hope it fits with your code.

zlan_1_cooper  Wednesday, October 24, 2007 3:42 PM

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

DataGridView1.EndEdit()

'

'

End Sub
lovzan  Saturday, March 15, 2008 6:50 PM

I have not found a good resolution to this yet... at least not with keyboard compatability considered.

I would like to force dirty state changes, but not when moving down a combobox list to select the new state!

Older Post:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=597491&SiteID=1

MS reference on dirty states:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx

When tabbing through the grid, if the user hits <Alt><Down> and then keys though the combobox list, the dirty state is also changed. So as I see it, there are two possibilities left:

1) Configure the combobox not to change right away when simply browsing the list. Maybe detach editing mode or something!?

2) Override the keypress event and don't process dirty state changes if initiated by a key. This seems "easier" for the end user.

Any more coding options here?

It seems MS should provide this since the real event (OnDropDownClosed) is not available to developers.

Also, when using the mouse this time... why does it takes two or even three clicks to show a drop down!

Bill

Bill 24601  Wednesday, September 17, 2008 10:11 PM
JonM wrote:
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?


I have a similar problem: DataGridViewCheckBoxColumn.

When I press space, checkbox checks ok, but when I press enter, and space again it doesn't work, check box looks like it is frozen. When i go to next cell(change focus), it works all right, but after pressing enter, space doesn't work after that. It looks as it is read only, but it's not.

Help anyone?


MarkoDatagridviewYeah  Thursday, November 06, 2008 12:50 PM

There's an easy way to get DataGridViewCheckBoxCell and DataGridViewComboBoxCell cells to commit changes without having to move focus to another cell. I found out how to do it by reading the class introduction for DataGridViewCheckBoxCell in MSDN. Capture the "CurrentCellDirtyStateChanged" event for the DataGridView and inside the handler:

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)

{

if (dataGridView1.CurrentCell is System.Windows.Forms.DataGridViewCheckBoxCell ||

dataGridView1.CurrentCell is System.Windows.Forms.DataGridViewComboBoxCell)

{

dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

}

}

This generates the "CellValueChanged" event on the grid and will also update any items bound to the check box and combo box cells.

It worked great for me. I hope it fits with your code.

This solution solved my issue which I believe was similar to the original poster's problem.

Thanks for your help.

Martin
martman.roy  Thursday, September 17, 2009 10:52 PM

You can use google to search for other answers

Custom Search

More Threads

• Filling DataSet Problems.
• Hide item in ComboBox
• Refresh DataSource of DataGridView Inside ToolStripDropDown
• datagrid
• Timer problem with windows service
• System.NullReferenceException: Object reference not set to an instance of an object
• DataGrid saving modified cell values?
• Right click event from a ListView item
• Data Navigation with OleDb
• "copy if newer" database property not working!!!!!!!!!