在C#中捕获datetimepicker的无效值

时间:2017-01-20 17:47:47

标签: c# mysql

我在数据库的textbox和datetimepicker中显示数据。但是有些记录是空的,并且在datetimepicker中显示无效。我想捕获每个空日期以在datetimepicker中设置null。到目前为止我有这个代码:

   if ((myReader["Born"].ToString()) == "" &&      (myReader["Died"].ToString()) != "" && (myReader["Interment"].ToString()) != "")
                {
                    textBox1.Text = (myReader["Surname"].ToString());
                    textBox2.Text = (myReader["FirstName"].ToString());
                    textBox3.Text = (myReader["MI"].ToString());
                    textBox4.Text = (myReader["Sex"].ToString());
                    dateTimePicker1.Format = DateTimePickerFormat.Custom;
                    dateTimePicker1.CustomFormat = " ";
                    dateTimePicker2.Value = Convert.ToDateTime(myReader["Died"].ToString());
                    textBox7.Text = (myReader["Age"].ToString());
                    dateTimePicker3.Value = Convert.ToDateTime(myReader["Interment"].ToString());
                    textBox9.Text = (myReader["Area"].ToString());
                }
                else if ((myReader["Died"].ToString()) == "" && (myReader["Born"].ToString()) != "" && (myReader["Interment"].ToString()) != "")
                {
                    textBox1.Text = (myReader["Surname"].ToString());
                    textBox2.Text = (myReader["FirstName"].ToString());
                    textBox3.Text = (myReader["MI"].ToString());
                    textBox4.Text = (myReader["Sex"].ToString());
                    dateTimePicker1.Value = Convert.ToDateTime(myReader["Born"].ToString());
                    dateTimePicker2.Format = DateTimePickerFormat.Custom;
                    dateTimePicker2.CustomFormat = " ";
                    textBox7.Text = (myReader["Age"].ToString());
                    dateTimePicker3.Value = Convert.ToDateTime(myReader["Interment"].ToString());
                    textBox9.Text = (myReader["Area"].ToString());
                }
                else if ((myReader["Interment"].ToString()) == "" && (myReader["Born"].ToString()) != "" && (myReader["Died"].ToString()) == "")
                {
                    textBox1.Text = (myReader["Surname"].ToString());
                    textBox2.Text = (myReader["FirstName"].ToString());
                    textBox3.Text = (myReader["MI"].ToString());
                    textBox4.Text = (myReader["Sex"].ToString());
                    dateTimePicker1.Value = Convert.ToDateTime(myReader["Born"].ToString());
                    dateTimePicker2.Value = Convert.ToDateTime(myReader["Died"].ToString());
                    textBox7.Text = (myReader["Age"].ToString());
                    dateTimePicker3.Format = DateTimePickerFormat.Custom;
                    dateTimePicker3.CustomFormat = " ";
                    textBox9.Text = (myReader["Area"].ToString());
                }

2 个答案:

答案 0 :(得分:0)

“我想在datetimepicker中设置每个空日期以设置null。到目前为止我有这个代码” - 下面的代码与原始代码做同样的事情,但是应该处理无效日期的其他情况。

“此代码有效,我可以将datetimepicker设置为null但不完整且太长。我想最小化它并完全捕获无效。” 原始代码没有将其设置为null,下面的代码执行相同的操作。

“我想最小化它并完全陷入无效” - 这就是下面的代码所做的事情。

textBox1.Text = myReader["Surname"].ToString();
textBox2.Text = myReader["FirstName"].ToString();
textBox3.Text = myReader["MI"].ToString();
textBox4.Text = myReader["Sex"].ToString();
textBox7.Text = myReader["Age"].ToString();
textBox9.Text = myReader["Area"].ToString();
trySetDateTimePickerValue(myReader["Born"], dateTimePicker1);
trySetDateTimePickerValue(myReader["Died"], dateTimePicker2);
trySetDateTimePickerValue(myReader["Interment"], dateTimePicker3);

// ...

public void trySetDateTimePickerValue(DateTimePicker dateTimePicker, SomeTypeOfReader reader)
{
    if (reader != null)
    {
        string cleanDate = reader.ToString().Trim(); 
        if (cleanDate.Length > 0) // maybe this trim/test is overkill, maybe the TryParse below is sufficient
        {
            DateTime parsedDate = DateTime.MinValue;
            if (DateTime.TryParse(cleanDate, out parsedDate)
            {
                dateTimePicker.Value = parsedDate;
                return; // success - exit method
            }
        }
    }

    // no valid date in string:

    // this is the default value if no value is set according to MSDN
    dateTimePicker.Value = DateTime.Now;

    // show nothing in the picker (not sure if this is good solution, this is what the original code does)
    dateTimePicker.Format = DateTimePickerFormat.Custom;
    dateTimePicker.CustomFormat = " ";

}

代码维护原始的坏对象名称(textBox1,textBox2,...)

另请注意,代码具有文化敏感性,日期时间解析在不同区域设置中的行为可能会有所不同(就像原始代码一样)。为日期时间解析方法提供额外的参数,以便在需要时指定文化。

答案 1 :(得分:-1)

WOuld强烈建议重构代码,你有几乎相同的代码块重复三次,并使代码极难阅读。打破相同的部分,只在if子句中加入不同的部分。

至于解决实际问题,这样的事情应该有效:

setDateTimerPickerValueIfExists(dateTimePicker1, myReader["Born"]);
setDateTimerPickerValueIfExists(dateTimePicker2, myReader["Died"]);

//..

private void setDateTimerPickerValueIfExists(DateTimePicker dateTimePicker, xxxxxxxReader reader) {
    if (reader != null) {
        dateTimePicker.Value = Convert.ToDateTime(reader.ToString());
    }
}