Click here to Skip to main content
15,794,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

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;
                    }
                }


[edit]"inline code" replaced with "code block" to preserve formatting - OriginalGriff[/edit]
Posted
Updated 24-Jul-10 22:41pm
v2
Comments
OriginalGriff 25-Jul-10 3:45am    
Rule 1 - don't use "magic numbers". When you define the columns, you give them names. Use them instead of Cells[3], it makes it all more readable. Cells[3].Value or Cells["JoinDate"].Value - which would you rather read?
Rule 2 - why check for true, then have an else which checks for false?

1 solution

I think you will find there is a difference between "null" and "string.Empty". Try using string.IsNullOrEmpty(mystring) instead of mystring == null and see if that changes things.
 
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