Hi,

Can someone help me pls why this doesn't seem to work?

private void dataGridViewempl_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
            if (dataGridViewempl.CurrentCell.ColumnIndex == 6)
            {
                CheckBox deletebox = new CheckBox();
                deletebox = (CheckBox)dataGridViewempl.CurrentCell.Value;

                if (deletebox != null)
                {
                    if (deletebox.Checked == false)
                        deletebox.Checked = true;
                    else
                        deletebox.Checked = false;
                }

            }
           
        }

Really appreciate it. Thanks much!

Recommended Answers

All 17 Replies

Here:

private void Form5_Load(object sender, EventArgs e)
{ 
       DataGridViewComboBoxColumn cmbcolumn = new DataGridViewComboBoxColumn(); 
       cmbcolumn.Name = "cmbColumn";
       cmbcolumn.HeaderText = "combobox column"; 
       cmbcolumn.Items.AddRange(new string[] { "item1", "item2", "item3" });  //here you can even use dataBinding!
       dataGridView1.Columns.Insert(1, cmbcolumn); //or add: dgv.Columns.Add(cmbcolumn);
       dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);  
}

private void dataGridViewEkipe_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
     ComboBox combo = e.Control as ComboBox;
     if (combo != null)
     {
          // Remove an existing event-handler, if present, to avoid 
          // adding multiple handlers when the editing control is reused.
          combo.SelectedIndexChanged -=
          new EventHandler(ComboBox_SelectedIndexChanged);

          // Add the event handler. 
          combo.SelectedIndexChanged +=
          new EventHandler(ComboBox_SelectedIndexChanged);
     }
}

private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
     ComboBox cb = (ComboBox)sender;
     string item = cb.Text;
     if (item != null)
         MessageBox.Show(item);
}

This is it!

You created a variable deletebox in a method.
After the method is finished, deletebox does not exist any more.

place CheckBox deletebox = new CheckBox(); before your if condition

CellContentClicked is not suitable event to get comboBox clciked (or selected). You you will never get this code into work. Take a look my example, you have to use CheckBox_SelectedIndexChanged event, but since DGV does NOT have this kind of event, you have to do a work around, to use a "normal" comboBox event.

It works like charm :)

Hi all,

Thanks for your feedback. However, none of them didn't do it. I don't understand why this is so difficult when this is relatively easy in VB :=) Even I can't find any solution in the internet.

@Mitja -- Thanks! But I don't understand; your code involves a combobox, not a checkbox? I'm probably missing something..

Still trying to tweak my code..

OMG, I just found the solution. My code is correct, the property of my datagrid is not. EditMode should have been EditOnEnter. I set it as EditProgrammatically because I'm so used to doing that :=)

Whew, I'm just so relieved. Thanks all!

Oh OK. I'm not relieved after all :=( EditOnEnter lets me edit any of my columns and I don't want that. This is getting frustrating...

Oh OK. I'm not relieved after all :=( EditOnEnter lets me edit any of my columns and I don't want that. This is getting frustrating...

Sorry, I gave you the wrong code, it was for ComboBox column, sorry ones again.

What is your problem now? Can you please explain it a bit better?

Hi ddanbe,
Thanks for pointing the article. I've revised my code, but still it doesn't work:

if (dataGridViewempl.CurrentCell.ColumnIndex == 6)
            {
                if (dataGridViewempl.CurrentCell.FormattedValueType == Type.GetType("System.String"))
                if (dataGridViewempl.CurrentCell.Value != null)
                {
                    bool checkstate = (bool)dataGridViewempl.CurrentCell.Value;

                    if (checkstate == false)
                        dataGridViewempl.CurrentCell.Value = true;
                    else
                        dataGridViewempl.CurrentCell.Value = false;

                }
                else
                    dataGridViewempl.CurrentCell.Value = true;

            }

@Mitja -- that's alright :=) I'm still trying to solve this.

Oops, sorry, wrong code..I was tweaking and adding/removing and re-arranging my code, guess I did not remove some..Here's the correct one:

if (dataGridViewempl.CurrentCell.ColumnIndex == 6)
            {
                
                if (dataGridViewempl.CurrentCell.Value != null)
                {
                    bool checkstate = (bool)dataGridViewempl.CurrentCell.Value;

                    if (checkstate == false)
                        dataGridViewempl.CurrentCell.Value = true;
                    else
                        dataGridViewempl.CurrentCell.Value = false;

                }
                else
                    dataGridViewempl.CurrentCell.Value = true;

            }

And it doesn't work at all still..

try this code

{
//your code
this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridView1_CellClick);
}
void dataGridView1_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
           {
               //  MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());
               try
               {
                   if (dataGridView1.Rows[e.RowIndex].Cells["checkbox"].Value == null || dataGridView1.Rows[e.RowIndex].Cells["checkbox"].Value.ToString().ToUpper() == "FALSE")
                       dataGridView1.Rows[e.RowIndex].Cells["checkbox"].Value = true;
                   else
                       dataGridView1.Rows[e.RowIndex].Cells["checkbox"].Value = false;


               }
               catch (Exception str)
               {
                   MessageBox.Show(str.Message);
               }

           }

@abelLazm: OMG! You saved me, you you! I'm so so grateful :=) I've been reading msdn and about DataGridView and explored their sample codes there on the off-chance something will work but just made my head ache. And here you are with this clean code...

Questions:
1. What does this mean?

this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridView1_CellClick);

2. It doesn't work if I cast it into a checkbox, like what I did with my code. And so now I'm curious why it has to be .Value.ToString().ToUpper() == "FALSE" and not just .Value.ToString() == "False" or "false".

I'm so relieved with your solution. Again, thanks so much!

You are Welcome :)
Answer number one:

this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridView1_CellClick);

is the event generated when you click on any cell of Datagrid like you were using cellcontentclick

Answer number two: I used .Value.ToString().ToUpper() == "FALSE" because i was not sure about, is it False or false when converted to string.

If you Got your solution please mark this thread as solved :)

Oops, OK, I just found out I can't cast it into a checkbox, but I can cast it into a boolean type, the same code I used above in reply to @ddanbe.

And so, here's the complete code that works now:

private void FormMain_Load(object sender, EventArgs e)
        {
Class1.displayempl(dataGridViewempl);
            dataGridViewempl.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridViewempl_CellClick);
}

private void dataGridViewempl_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
if (dataGridViewempl.CurrentCell.ColumnIndex == 6)
                    {
                        if (dataGridViewempl.CurrentCell.Value != null)
                        {
                        dataGridViewempl.BeginEdit(true);
                        bool checkstate = (bool)dataGridViewempl.CurrentCell.Value;

                        if (checkstate == false)
                            dataGridViewempl.CurrentCell.Value = true;
                        else
                            dataGridViewempl.CurrentCell.Value = false;
                        }
                        else

                    dataGridViewempl.CurrentCell.Value = true;
                    }
}

To @ddanbe and @abelLazm, thanks so much for the solutions. And to all the others, for your time :=)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.