C# - 如何在DateTimePicker上禁用某些日期?

时间:2016-08-22 14:57:10

标签: datetimepicker

我有两个dateTimePicker,我在第一个和第二个选择日期,它会自动阻止选择比第一个更早的日期。 我使用了如下所示的代码,但日期没有显示

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    //TODO SOMETHING (handle errors etc...)
    //...        

    dateTimePicker2.MinDate = dateTimePicker1.Value;
}
我希望展示一天,但残疾人不能选择?可以这样吗?请!

enter image description here

我的代码如下: Source code

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;

namespace formStudent
{
    public partial class formStudent : Form
    {
        List<Student> listStudent = new List<Student>();
        class Student
        {
            public string name { get; set; }
            public string gender { get; set; }
            public string birthday { get; set; }

            public Student(string inName, string inGender, string inBirthday)
            {
                name = inName;
                gender = inGender;
                birthday = inBirthday;
            }
        }
        public formStudent()
        {
            InitializeComponent();

            string[,] arrData = new string[10, 3] {
                                    { "Jane","Female","2016/08/11" },
                                    { "Peter","Female","2016/08/12" },
                                    { "John","Female","2016/08/13" },
                                    { "Ronaldo","Male","2016/08/14" },
                                    { "Jerry","Female","2016/08/15" },
                                    { "David","Female","2016/08/16" },
                                    { "Rooney","Male","2016/08/17" },
                                    { "Ozil","Male","2016/08/18" },
                                    { "Torres","Male","2016/08/19" },
                                    { "Messi","Male","2016/08/20" },
                                 };

            //Data row
            List<string> dataRow = new List<string>();

            //Data Student
            Student student = null;

            for (int row = 0; row < 10; row++)
            {
                for (int col = 0; col < 3; col++)
                {
                    dataRow.Add(arrData[row, col]);
                }

                student = new Student(dataRow[0], dataRow[1], dataRow[2]);
                //Get data student
                listStudent.Add(student);

                dataRow = new List<string>();
            }
        }

        // Filter 1
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            this.dataGridView.Rows.Clear();

            var filteredData = listStudent.Where(st => ((dateTimePicker1.Value <= DateTime.Parse(st.birthday).AddDays(1)) && (dateTimePicker2.Value >= DateTime.Parse(st.birthday))));
            List<Student> listData = new List<Student>(filteredData);
            displayData(listData);

            /**
             * dateTimePicker2.MinDate = dateTimePicker1.Value;
             */

        }

        // Filter 2
        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            this.dataGridView.Rows.Clear();

            var filteredData = listStudent.Where(st => ((dateTimePicker1.Value <= DateTime.Parse(st.birthday).AddDays(1)) && (dateTimePicker2.Value >= DateTime.Parse(st.birthday))));
            List<Student> listData = new List<Student>(filteredData);
            displayData(listData);

            if (dateTimePicker2.Value < dateTimePicker1.Value.Date)
            {
                dateTimePicker2.Value = dateTimePicker1.Value.Date;
            }
        }

        private void dateTimePicker2_Enter(object sender, EventArgs e)
        {
            // temporarily set to an earlier date 
            dateTimePicker1.MinDate = new DateTime(1900, 1, 1);
        }

        private void dateTimePicker2_Leave(object sender, EventArgs e)
        {
            dateTimePicker2.MinDate = dateTimePicker1.Value.Date;
        }

        private void formStudent_Load(object sender, EventArgs e)
        {
            //Show data
            displayData(listStudent);
        }

        private void displayData(List<Student> listData)
        {
            //Show data
            foreach (Student item in listData)
            {
                int ii = this.dataGridView.Rows.Add();
                this.dataGridView.Rows[ii].Cells[0].Value = item.name;
                //Gender
                string strGender = string.Empty;
                if ("Male".Equals(item.gender))
                    strGender = "Male";
                else
                    strGender = "Female";
                this.dataGridView.Rows[ii].Cells[1].Value = strGender;
                this.dataGridView.Rows[ii].Cells[2].Value = item.birthday;

            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我知道这样做的唯一方法如下:

    private void dateTimePicker2_Enter(object sender, EventArgs e)
    {
        // temporarily set to an earlier date 
        dateTimePicker1.MinDate = new DateTime(1900, 1, 1);          
    }

    private void dateTimePicker2_Leave(object sender, EventArgs e)
    {
        dateTimePicker2.MinDate = dateTimePicker1.Value.Date;
    }

    private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
    {
        if (dateTimePicker2.Value < dateTimePicker1.Value.Date)
        {
            dateTimePicker2.Value = dateTimePicker1.Value.Date;
        }
    }

如果您这样做,那么较早的日期不会显示为灰色(禁用),但是任何尝试点击较早的日期,只会导致将日期设置为最小值。