开发者

DateTimePicker is not working Properly

开发者 https://www.devze.com 2023-02-19 20:59 出处:网络
Actually , I have 2 dateTimePicker in my form in c sharp dot net 2008.Both of them perform same event.But One of them is not working Properly until i use another one.Please Help me OUT!!!!

Actually , I have 2 dateTimePicker in my form in c sharp dot net 2008.Both of them perform same event.But One of them is not working Properly until i use another one.Please Help me OUT!!!!

    private void dtpStart_ValueChanged(object sender, EventArgs e)
    {

        if (cmbDay.SelectedIndex == -1 || cmbLeaveName.SelectedIndex == -1)
        {
            MessageBox.Show("Please select Day and Leave Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            dtpStart.ValueChanged -= new EventHandler(dtpStart_ValueChanged);

        }
        if (dtpStart.Value > dtpEnd.Value)
        {
              MessageBox.Show("The End date of leave cannot be occur before date of leave ", "Invalid Entry", MessageBoxButtons.OK);
            dtpStart.Value = dtpEnd.Value;
        }
        getdays();
        check = validate();
        if (check == "Incorrect")
        {
            check = "Correct";
            return;
        }
        LoadDataGridView();

    }


   private void dtpEnd_ValueChanged(object sender, EventArgs e)
    {
        if (cmbDay.SelectedIndex == -1 || cmbLeaveName.SelectedIndex == -1)
        {
            MessageBox.Show("Please select Day and Leave Name","Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
            dtpEnd.ValueChanged -= new EventHandler(dtpEnd_ValueChanged);
            return;

        }
        if (dtpEnd.Value < dtpStart.Value)
        {
            MessageBox.Show("The End date of leave cannot be occur before date of leave ", "Invalid Entry", MessageBoxButtons.OK);
            dtpEnd.Value = dtpStart.Value;

        }
        getdays();
        check = validate();
        if (check == "Incorrect")
        {
            check = "Correct";
            return;
        }
        LoadDataGridView();

    }



    private void getdays()
    {
        double ts = GetDateDifference();
        if (cmbDay.Text.ToString() == "Full Day")
        {
            txtLeaveApplied.Text = ts.ToString();
        }
        else if (cmbDay.Text.ToString() == "Half Day")
        {
            txtLeaveApplied.Text = ((float.Parse(ts.ToString())) / 2).ToString();
        }

    }
    private string validate()
    {
        string Name = cmbApplicantName.Text.ToString();
        string EMP_ID = GetEmpId(Name);
        DataTable dtvalidate = new DataTable();

        dtvalidate = LI.ValidateLeaveInfo(EMP_ID,
           DateTime.Parse(dtpStart.Value.ToShortDateString()),
           DateTime.Parse(dtpEnd.Value.ToShortDateString()));
        if (dtvalidate.Rows.Count > 0)
        {
            StringBuilder开发者_运维技巧 date = new StringBuilder();
            foreach (DataRow row in dtvalidate.Rows)
            {
                date.Append(row["Leave_Date"].ToString() + Environment.NewLine);
            }
            MessageBox.Show("Leave Already applied in following Date(s)" +
                Environment.NewLine + date, "Select valid date", MessageBoxButtons.OK,
                MessageBoxIcon.Exclamation);
            check = "Incorrect";
            dgvLeaveApplication.Rows.Clear();

        }
        dtvalidate.Rows.Clear();
        dtvalidate.Dispose();
        return check;
    }


    private void LoadDataGridView()
    {

        double ts = GetDateDifference();
        dgvLeaveApplication.Rows.Clear();
        DateTime dt = DateTime.Parse(dtpStart.Value.ToShortDateString());
        for (int i = 0; i < Convert.ToInt32(ts.ToString()); i++)
        {
            dgvLeaveApplication.Rows.Add(dgvLeaveApplication.Rows.Count,
                dt.ToShortDateString(), cmbLeaveName.SelectedValue.ToString());

            dt = dt.AddDays(1);

        }
        dgvLeaveApplication.Refresh();
    }


Check AutoPostBack property for both components.


You have some very strange constructions in your code. E.g.,

check = validate();
if (check == "Incorrect")
{
    check = "Correct";
    return;
}

(Why not have Validate() return a bool? And why are you ignoring validation errors?) My advise is to restructure, rename and change signatures of your methods so the code becomes readable. That will make it a lot easier for you to find mistakes.

Did you step through the code using the debugger? Did the event get raised for both pickers?

EDIT You remove the handler but never add it. So after removing it you have to decide when to add it again. That won't happen by magic!

0

精彩评论

暂无评论...
验证码 换一张
取 消