按日期筛选数据案例

本文介绍了如何在C#的WindowsForms应用程序中创建一个Form,用于管理Student对象列表,包括数据绑定、DateTimePicker控件的事件处理以及根据特定日期区间筛选学生信息的功能。
摘要由CSDN通过智能技术生成

需要添加一个student类

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

namespace Demo7
{
    public partial class Form3 : Form
    {
        List<Student> students = null;
        public Form3()
        {
          
            InitializeComponent();

            BindDataGradView(); //绑定list数据

            //开始时默认将俩个dateTimePicker内容置空
            this.dtpStart.Format = DateTimePickerFormat.Custom;
            this.dtpStart.CustomFormat = " ";

            this.dtpEnd.Format = DateTimePickerFormat.Custom;
            this.dtpEnd.CustomFormat = " ";
        }

        private void BindDataGradView() {
            students = new List<Student>()
           {
            new Student() { Id = 1, Name = "张三", Age = 18, Sex = "男", bridaty = new DateTime(2024, 2, 15) },
            new Student() { Id = 2, Name = "张三", Age = 18, Sex = "男", bridaty = new DateTime(2024, 3, 15) },
            new Student() { Id = 3, Name = "张三", Age = 18, Sex = "男", bridaty = new DateTime(2024, 4, 15) },
            new Student() { Id = 4, Name = "张三", Age = 18, Sex = "男", bridaty = new DateTime(2024, 5, 15) }

            };
            //在dataGridView1中显示数据并更改列名
            dataGridView1.DataSource = students;
            dataGridView1.Columns[0].HeaderText = "学生ID";
            dataGridView1.Columns[1].HeaderText = "学生姓名";
            dataGridView1.Columns[2].HeaderText = "学生年龄";
            dataGridView1.Columns[3].HeaderText = "学生性别";
            dataGridView1.Columns[4].HeaderText = "学生生日";


        }

        //改变两个dateTimePicker的value时通过改变格式将日期显示
        private void dtpStart_ValueChanged(object sender, EventArgs e)
        {
            this.dtpStart.Format = DateTimePickerFormat.Long;
            this.dtpStart.CustomFormat = null;


        }

        private void dtpEnd_ValueChanged(object sender, EventArgs e)
        {
            this.dtpEnd.Format = DateTimePickerFormat.Long;
            this.dtpEnd.CustomFormat = null;

        }

       //得到该时间区间内符合的数据
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string sarttime = dtpStart.Text + "00:00:00";
            string endtime = dtpEnd.Text + "23:59:59";

            DateTime Sart = DateTime.Parse(sarttime);
            DateTime End = DateTime.Parse(endtime);

            List<Student> filesStudent = students;

            if (!string.IsNullOrWhiteSpace(dtpStart.Text))
            {
                filesStudent = filesStudent.Where(s => s.bridaty >= Sart).ToList();
            }
            if (!string.IsNullOrWhiteSpace(dtpEnd.Text))
            {
                filesStudent = filesStudent.Where(s => s.bridaty <= End).ToList();
            }

            dataGridView1.DataSource = filesStudent;
        }

        //清空将两个dateTimePicker清空方便重新搜索 清空时需将日期恢复成当前日期
        private void btnClear_Click(object sender, EventArgs e)
        {
            dtpStart.Value = DateTime.Now;
            dtpStart.Format = DateTimePickerFormat.Custom;
            dtpStart.CustomFormat = " ";

            dtpEnd.Value = DateTime.Now;
            dtpEnd.Format = DateTimePickerFormat.Custom;
            dtpEnd.CustomFormat = " ";
        }
    }
}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值