Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > datagridviewcheckboxcell problem
 

datagridviewcheckboxcell problem

I have a datagridview (VS 2005) with columns of which one is datagridviewComboboxcell and another of type
datagridviewCheckboxcell. all columns generated at runtime.
Now if i check a datagridviewCheckboxcell it will first check whether a selected item of datagridviewComboboxcell is there
or not.
If it is there it will remain checked or it will get unchecked.

help me in C#
bunty_20k  Tuesday, August 18, 2009 9:53 AM
Create a CurrenCellDirtyStateChanged event handler for your DataGridView and the in the handler write like this..

private void grid1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
// considering row 2 is your ComboBox cell
if(grid1.CurrentRow.Cells[1].Value == null)
{
bool value = (bool)( grid1.CurrentCell.Value ?? false );
grid1.CurrentCell.Value = !value;
}
else
{
MesageBox.Show("Please select a value in the combobox first");
}
} 
Dunno if this will work or not.. but give it a try.

Life would have been much easier if I had the source-code !!
Mohib Sheth  Tuesday, August 18, 2009 10:19 AM

Hi bunty_20k,

To achieve your goal, you can handle the CellValueChanged to change the value of the CheckBox cell. This is the code snippet:
private void Form1_Load(object sender, EventArgs e)

{

//Handle CellValueChanged event to set the value of the CheckBoxCell.

this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

}

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)

{

if (e.RowIndex >= 0 && e.ColumnIndex == this.checkColumn.Index)

{

this.dataGridView1.CellValueChanged -= new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

//Change the value of the checkBox cell according to whether comboBox cell has a value.

if (this.dataGridView1.CurrentRow.Cells[this.comboColumn.Index].Value != null)

this.dataGridView1.CurrentCell.Value = true;

else

this.dataGridView1.CurrentCell.Value = false;

this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

}

}

But based on my understanding, you want the CheckBox column to show whether the ComboBox cell in the same row has a value or not. In this case, we need to have the CheckBox column read only and handle the CellValueChanged event to check/uncheck the CheckBox cell according to whether the ComboBox cell has a value or not. This is the code snippet:
private void Form1_Load(object sender, EventArgs e)

{

//Set check column read only to avoid editing.

this.checkColumn.ReadOnly = true;

//Handle CellValueChanged event to set the value of the CheckBoxCell.

this.dataGridView1.CellValueChanged +=new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

}

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)

{

if (e.RowIndex >= 0 && e.ColumnIndex == this.comboColumn.Index)

{

this.dataGridView1.CellValueChanged -= new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

//If comboBox has value, check the checkBox, elase uncheck it.

if (this.dataGridView1.CurrentCell.Value != null)

this.dataGridView1.CurrentRow.Cells[this.checkColumn.Index].Value = true;

else

this.dataGridView1.CurrentRow.Cells[this.checkColumn.Index].Value = false;

this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

}

}

Regards,

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, August 20, 2009 7:25 AM
Hi bunty_20k,

Could you tell me if our replies help you or not?

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, August 24, 2009 2:56 AM

Hi,

We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Tuesday, August 25, 2009 10:43 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView produces empty entry in underlying list
• keep column order in datagridview same as in sql after second query
• Datagridview find text(Search)
• I need create a master - details with a combobox and a gridcontrol
• VS 2005 - Filtering date with BindingSource in Access
• ComboBox Settings Binding
• Databinding of datagrid & combo box
• vb ado 2 question on binding bit
• How To: Press OK button with Enter key when DataGridView has focus
• the backgroud color of one column