Search code examples
c#if-statementdatagridviewdatetimepicker

If condition with datetimepicker & datagridview cell date


I'm new to C# programming & i have lack of knowledge in coding. So please help me out here.

I have datagridview(hereafter dgv) without bind to any datasource. dgv is feeding data from some text boxes & datetimepicker(hereafter dtp) on the form. I want to check an if condition before the user enter the data to the dgv. Please see the code below,

private void FEAdd_Click(object sender, EventArgs e)
   {

       var boxes = new List<TextBox>
       {
           MtrtextBox1,
           PltrtextBox11,
           BvtextBox12,

       };

       if (boxes.Any(tb => string.IsNullOrEmpty(tb.Text)))
       {
           MessageBox.Show("Fill required fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
           MtrtextBox1.BackColor = Color.Red;
           BvtextBox12.BackColor = Color.Red;
       }
       else if (dgvFE.RowCount >= 1 && (Convert.ToDateTime(dgvFE.Rows[dgvFE.RowCount - 1].Cells[0].Value.ToString()) > dateTimePicker1.Value.Date))
           {
               MessageBox.Show("Current bill date cannot be less than the previous bill date.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
           }
       else
       {

           int n = dgvFE.Rows.Add();
           dgvFE.Rows[n].Cells[0].Value = dateTimePicker1.Value.ToString("dd-MM-yyyy");
           dgvFE.Rows[n].Cells[1].Value = MtrtextBox1.Text.ToString();
           dgvFE.Rows[n].Cells[3].Value = PltrtextBox11.Text.ToString();
           dgvFE.Rows[n].Cells[4].Value = BvtextBox12.Text.ToString();


           foreach (DataGridViewRow row in dgvFE.Rows)
           {
               dgvFE.Rows[row.Index].Cells[2].Value = Math.Round((Double.Parse(dgvFE.Rows[row.Index].Cells[4].Value.ToString()) / Double.Parse(dgvFE.Rows[row.Index].Cells[3].Value.ToString())), 2).ToString();

               if (n >= 1 && row.Index > 0)
               {
                   dgvFE.Rows[row.Index - 1].Cells[5].Value = Math.Round((Double.Parse(dgvFE.Rows[row.Index].Cells[1].Value.ToString()) - Double.Parse(dgvFE.Rows[row.Index - 1].Cells[1].Value.ToString())), 2).ToString();
                   dgvFE.Rows[row.Index - 1].Cells[6].Value = Math.Round((Double.Parse(dgvFE.Rows[row.Index - 1].Cells[5].Value.ToString()) / Double.Parse(dgvFE.Rows[row.Index - 1].Cells[2].Value.ToString())), 2).ToString();

               }


           }
           MtrtextBox1.Text = null;
           BvtextBox12.Text = null;
           MtrtextBox1.BackColor = Color.White;
           BvtextBox12.BackColor = Color.White;
       }

   }

User can enter first line of data to dgv without any problem. But if user enter less date than the Cell[0] date in dgv it shows my error messagebox. That is also ok. Then user should enter higher date than the Cell[0] in dgv. But here is the problem arise. Its always showing the messagebox even if the user selects higher date than the Cell[0] date on dgv. Can anyone help me out with this? I googled and nowhere found any solution. Sorry for my bad English.


Solution

  • We should pay attention to date format when converting string to date.

    Below code might work for you. I suggest add break point or alert statements to see actual runtime values, sometimes that gives you an answer.

       private void FEAdd_Click(object sender, EventArgs e)
       {
    
           var boxes = new List<TextBox>
           {
               MtrtextBox1,
               PltrtextBox11,
               BvtextBox12,
    
           };
    
           if (boxes.Any(tb => string.IsNullOrEmpty(tb.Text)))
           {
               MessageBox.Show("Fill required fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
               MtrtextBox1.BackColor = Color.Red;
               BvtextBox12.BackColor = Color.Red;
               return;
           }
           else if (dgvFE.Rows.Count > 0)          
             {
                 DateTime dt1 = DateTime.ParseExact(dgvFE.Rows[dgvFE.RowCount - 1].Cells[0].Value.ToString(), "dd-MM-yyyy", CultureInfo.InvariantCulture); // careful about date formats.
                 DateTime dt2 = dateTimePicker1.Value.Date;
    
                 if(dt1>dt2)
                 { 
                     MessageBox.Show("Current bill date cannot be less than the previous bill date.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                     return;
                 }      
             }
    
    
               int n = dgvFE.Rows.Add();
               dgvFE.Rows[n].Cells[0].Value = dateTimePicker1.Value.ToString("dd-MM-yyyy");
               dgvFE.Rows[n].Cells[1].Value = MtrtextBox1.Text.ToString();
               dgvFE.Rows[n].Cells[3].Value = PltrtextBox11.Text.ToString();
               dgvFE.Rows[n].Cells[4].Value = BvtextBox12.Text.ToString();
    
    
               foreach (DataGridViewRow row in dgvFE.Rows)
               {
                   dgvFE.Rows[row.Index].Cells[2].Value = Math.Round((Double.Parse(dgvFE.Rows[row.Index].Cells[4].Value.ToString()) / Double.Parse(dgvFE.Rows[row.Index].Cells[3].Value.ToString())), 2).ToString();
    
                   if (n >= 1 && row.Index > 0)
                   {
                       dgvFE.Rows[row.Index - 1].Cells[5].Value = Math.Round((Double.Parse(dgvFE.Rows[row.Index].Cells[1].Value.ToString()) - Double.Parse(dgvFE.Rows[row.Index - 1].Cells[1].Value.ToString())), 2).ToString();
                       dgvFE.Rows[row.Index - 1].Cells[6].Value = Math.Round((Double.Parse(dgvFE.Rows[row.Index - 1].Cells[5].Value.ToString()) / Double.Parse(dgvFE.Rows[row.Index - 1].Cells[2].Value.ToString())), 2).ToString();
    
                   }
    
    
               }
    
               MtrtextBox1.Text = null;
               BvtextBox12.Text = null;
               MtrtextBox1.BackColor = Color.White;
               BvtextBox12.BackColor = Color.White;      
    
       }