实际上,我在c sharp dot net 2008中有2个dateTimePicker。他们两个都执行相同的事件。但是其中一个没有正常工作,直到我使用另一个。请帮帮我!!!!
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();
}
答案 0 :(得分:1)
检查两个组件的AutoPostBack属性。
答案 1 :(得分:1)
您的代码中有一些非常奇怪的结构。如,
check = validate();
if (check == "Incorrect")
{
check = "Correct";
return;
}
(为什么没有Validate()返回一个bool?你为什么忽略验证错误?) 我的建议是重构,重命名和更改方法的签名,以便代码变得可读。这将使您更容易找到错误。
您是否使用调试器单步执行代码?两个采摘者的活动都被提升了吗?
修改强> 您删除处理程序但从不添加它。因此,在删除它之后,您必须决定何时再次添加它。魔术不会发生这种情况!