judithSampathwa -2 Posting Pro in Training

Hi there,
I have a question relating to datagrid view in C#. I have a datagrid view in a form with a calendar column a check box column and a text box column. What I want to do is when the user clicks on the datagrid view check box column(column 4) the current date and time should display in the datagrid view text box column(column5). For this the user has to select a date from the calendar column (column3 which is a calendar column) to select the datagridview check box column.
My question is if the user selects the datagrid view checkbox column without selecting the Due date(column3) the user cannot select the datagridview check box column an error msg should display saying “Enter Due date first”. So for this scenario I wrote the code in CurrentCellDirtyStateChanged but the thing is the msg box apears two times and if the user didn’t select the due date and select the check box column the msg box is displayed two times and the check box is being selected.
The code I wrote is below

private void dgvActions_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            int row = dgvActions.CurrentCell.RowIndex;

            if (dgvActions.CurrentCell == null)
                return;
                        
           
                    if (dgvActions.CurrentCell.ColumnIndex == 5)
                    {
                        dgvActions.CommitEdit(DataGridViewDataErrorContexts.Commit);

                        if (dgvActions.CurrentCell.Value != null)
                        {
                            if (dgvActions[4, row].Value != null)
                            {

                                DateTime DueDate = DateTime.Parse(dgvActions.Rows[row].Cells[4].Value.ToString());

                                if (DateTime.Parse(dgvActions.Rows[row].Cells[4].Value.ToString()) < System.DateTime.Now.Date)
                                {
                                    MessageBox.Show("invalid date");
                                    dgvActions.Rows[row].Cells[4].Value = System.DateTime.Now.Date.ToString();
                                    return;
                                }
                                else
                                {
                                    if ((dgvActions.CurrentCell.Value == DBNull.Value) || ((bool)dgvActions.CurrentCell.Value == false))
                                    {
                                        dgvActions.CurrentRow.Cells["Column5"].Value = DBNull.Value;
                                    }
                                    else
                                    {
                                        dgvActions.CurrentRow.Cells["Column5"].Value = System.DateTime.Now;
                                    }
                                }
                            }
                            else
                            {
                                MessageBox.Show("Enter the Due date first");
                                dgvActions.CurrentRow.Cells["Column4"].Value = false;
                            }
                       }      

                   }
        }

thanxxxxxxx

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.