Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridViewCheckBoxColumn value not being set in data source
 

DataGridViewCheckBoxColumn value not being set in data source

I have a DataGridView which I am binding to a list of objects. One of the properties is a boolean so I am using a DataGridViewCheckBoxColumn to show the data.

When I check the box on any line, the value is not saved to the under-lying data object. It is only saved to the object when I tab out of the column which is no good as the user will not be aware of this.

I have tried attaching to the CellClick event on the grid and if the column is a DataGridViewCheckBox then I call the EndEdit() method but this does not work properly and instead makes the UI a bit clunky (needs 3 clicks to set and 3 clicks to un-set).

Any ideas on how I can get round this?

Regard,

Richard.
rhooper_imeta  Wednesday, February 20, 2008 10:27 AM

You can handle the CurrentCellDirtyStateChanged event to commit the changes.


Code Snippet

dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)

{

if (this.dataGridView1.CurrentCell.ColumnIndex ==

this.dataGridView1.Columns["Your CheckBox Column Name"].Index)

{

this.dataGridView1.EndEdit(DataGridViewDataErrorContexts.Commit);

}

}

Zhi-Xin Ye  Tuesday, February 26, 2008 2:28 AM

You may wish to investigate using the CellValueChanged event.

Hope this helps...

jbaird-pa  Wednesday, February 20, 2008 3:21 PM

You can handle the CurrentCellDirtyStateChanged event to commit the changes.


Code Snippet

dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)

{

if (this.dataGridView1.CurrentCell.ColumnIndex ==

this.dataGridView1.Columns["Your CheckBox Column Name"].Index)

{

this.dataGridView1.EndEdit(DataGridViewDataErrorContexts.Commit);

}

}

Zhi-Xin Ye  Tuesday, February 26, 2008 2:28 AM

You can use google to search for other answers

Custom Search

More Threads

• I want to delete the data from datagridview by using check box which should also affect in data base.
• Fixed Length Text File Editor using DataGridView in C#
• EditMode.EditProgrammatically
• TableAdapter.Update problem
• Binding Navigator - Enable/Disable Save Button
• Textbox Validating
• DataGridView
• DataGridViewComboBoxCell Data Binding
• Creating Collections in Class
• strange behavior after using “Configure Dataset with Wizardâ€