C#如何使用datetimepicker将<list>数据过滤到dataGridView中

时间:2016-08-16 14:46:11

标签: list datagridview filter

我有一个包含Student对象的List。加载表单时,数据显示在Datagridview控件中。 不要使用按钮来过滤基于我的2 datetimepicker(datefrom和dateto)的日期范围的数据

Image Description

以下是我的代码。你能帮我解决一下这个问题吗?谢谢!

Source code

using System;
using System.Collections.Generic;
using System.Windows.Forms;
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 array
            List<List<string>> listData = new List<List<string>>();

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

                listData.Add(dataRow);
                dataRow = new List<string>();
            }

            Student student = null;
            foreach (List<string> data in listData)
            {
                student = new Student(data[0], data[1], data[2]);

                //Get data student
                listStudent.Add(student);
            }
        }

        private void formStudent_Load(object sender, EventArgs e)
        {
            //Show data
            foreach (Student item in listStudent)
            {
                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;

            }
        }
    }
}

0 个答案:

没有答案