Hello everyone,

I am facing a problem with C# code. The problem is I have developed a form with DataGridView Control. In that grid view at the last cell of each row I have positioned a DataGridViewCheckBoxColumn. Now I wish to set some data in the DataGridViewTextBoxColumn of each row when I check the checkbox and when I uncheck the checkbox the data from that row of each text box column will be clear. I can set the data in each row to check the check box of that row but when I uncheck that check box the data is not set empty. Will you please help me to find the solution for this problem. For your better understanding I am sending the code. Please help me to fix the problem.

Thanks.

Code is:

int clmnIndex = e.ColumnIndex;
            int rowIndex = e.RowIndex;

            if (clmnIndex == 8)
            {
                DataGridViewCheckBoxCell chkBox = 
                    (DataGridViewCheckBoxCell)dgvAttendance.Rows[rowIndex].Cells[8];
                
                if (chkBox.Value == chkBox.TrueValue)
                {
                    if (dgvAttendance.Rows[rowIndex].Cells[7].Value == null)
                    {                        
                        dgvAttendance.Rows[rowIndex].Cells[3].Value = DateTime.Now.ToString("MMM dd, yyyy");
                        dgvAttendance.Rows[rowIndex].Cells[4].Value = DateTime.Now.ToString("HH:mm:ss");
                        dgvAttendance.Rows[rowIndex].Cells[7].Value = "CheckIn";
                    }
                    else
                    {
                        dgvAttendance.Rows[rowIndex].Cells[5].Value = DateTime.Now.ToString("HH:mm:ss");
                    }
                }
                else if (chkBox.Value == chkBox.FalseValue)
                {
                    if (dgvAttendance.Rows[rowIndex].Cells[7].Value == null)
                    {
                        dgvAttendance.Rows[rowIndex].Cells[3].Value = string.Empty;
                        dgvAttendance.Rows[rowIndex].Cells[4].Value = string.Empty;
                        dgvAttendance.Rows[rowIndex].Cells[7].Value = string.Empty;
                    }
                    else
                    {
                        dgvAttendance.Rows[rowIndex].Cells[5].Value = string.Empty;
                    }
                }

When you check the checkbox cell you set col 7 to "CheckIn".
When you uncheck the checkbox cell you test col 7 for null.
You need to re-think the test logic again.

int clmnIndex = e.ColumnIndex;
            int rowIndex = e.RowIndex;

            if (clmnIndex == 8)
            {
                DataGridViewCheckBoxCell chkBox = 
                    (DataGridViewCheckBoxCell)dgvAttendance.Rows[rowIndex].Cells[8];
                
                if (chkBox.Value == chkBox.TrueValue)
                {
                    if (dgvAttendance.Rows[rowIndex].Cells[7].Value == null)
                    {                        
                        dgvAttendance.Rows[rowIndex].Cells[3].Value = DateTime.Now.ToString("MMM dd, yyyy");
                        dgvAttendance.Rows[rowIndex].Cells[4].Value = DateTime.Now.ToString("HH:mm:ss");
                        [B]dgvAttendance.Rows[rowIndex].Cells[7].Value = "CheckIn";[/B] //<-- setting col 7
                    }
                    else
                    {
                        dgvAttendance.Rows[rowIndex].Cells[5].Value = DateTime.Now.ToString("HH:mm:ss");
                    }
                }
                else if (chkBox.Value == chkBox.FalseValue)
                {
                    [B]if (dgvAttendance.Rows[rowIndex].Cells[7].Value == null)[/B] //<-- testing col 7 for null
                    {
                        dgvAttendance.Rows[rowIndex].Cells[3].Value = string.Empty;
                        dgvAttendance.Rows[rowIndex].Cells[4].Value = string.Empty;
                        dgvAttendance.Rows[rowIndex].Cells[7].Value = string.Empty;
                    }
                    else
                    {
                        dgvAttendance.Rows[rowIndex].Cells[5].Value = string.Empty;
                    }
                }
This article has been dead for over six months. Start a new discussion instead.