Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
4.57/5 (3 votes)
See more:
Hi,
I have a DataGridView, which has one CheckBox column.
Now I want to handle CheckedChanged event of CheckBox in it.
I use following code
C#
private void dgrvProductTemplate_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (dgrvProductTemplate.CurrentCell.ColumnIndex == 1)
    {
        CheckBox chk;
        CheckBox myDgvCheckBox;
        chk = (CheckBox)e.Control;
        myDgvCheckBox = (CheckBox)e.Control;
        if (chk == null)
        {
            return;
        }
        chk.CheckedChanged += new EventHandler(ck_CheckedChanged);
    }
}

private void ck_CheckedChanged(object sender, EventArgs e)
{
    try
    {
        CheckBox chk = new CheckBox();
        chk = sender as CheckBox;
        if (chk.Checked == true)
        {
            dgrvProductTemplate[0, 1].Value = true;
        }
        else
        {
            dgrvProductTemplate[0, 1].Value = false;
        }
    }
    catch (Exception ex)
    {
    }
}

But when i check checkbox, it doesnt call, dgrvProductTemplate_EditingControlShowing event

Any Solutions, pls

[Code formatting]
Posted
Updated 25-Feb-10 23:15pm
v2

Hope this code will make you satisfy

C#
private void dgrvProductTemplate_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
                if (e.ColumnIndex == 2)//set your checkbox column index instead of 2
                {
                    if (Convert.ToBoolean(dgrvProductTemplate.Rows[e.RowIndex].Cells[2].EditedFormattedValue) == true)
                    {
                       //Paste your code here
                    }
                }
        }
 
Share this answer
 
Comments
warmdeveloper 3-Feb-16 13:22pm    
Thanks ! solved my issue
You calling CheckBox_CheckChanged event in dgrvProductTemplate_EditingControlShowing event.
So do the opposite.

C#
private void ck_CheckedChanged(object sender, EventArgs e)
{
    try
    {
        CheckBox chk = new CheckBox();
        chk = sender as CheckBox;
        if (chk.Checked == true)
        {
            dgrvProductTemplate[0, 1].Value = true;
            // Call your event here..
        }
        else
        {
            dgrvProductTemplate[0, 1].Value = false
    }
    catch (Exception ex)
    {
    }
}

-KR
 
Share this answer
 
Looks like you missed DataGrid's ItemCreated event.

Please follow the link, you will get it:
Handling events of child controls inside a DataGrid[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900