如何将日期选择器与datagridview集成
本文关键字:datagridview 集成 选择器 日期 | 更新日期: 2023-09-27 17:59:39
在我的应用程序中,我正在对员工进行查询,以了解每个员工的订单量。我想添加一个日期过滤器。所以我创建了两个日期时间选择器,并为它们构建了一个函数。其中&我应该如何在它们之间集成,或者我可以在查询中输入一些方法?
代码:
private void Form3_Load(object sender, EventArgs e)
{
this.employeesTableAdapter.Fill(this.nORTHWNDDataSet.Employees);
using (NorthWindDataContext db = new NorthWindDataContext())
{
var query =
from d in db.Employees
select new EmployeeData
{
FirstName = d.FirstName,
EmployeeID = d.EmployeeID
};
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = query;
}
}
public bool CheckDate(DateTime date)
{
DateTime floorDate = FromDate.Value;
DateTime roofDate = ToDate.Value;
if (date > floorDate && date < roofDate)
{
return true;
}
else
{
return false;
}
}
private void displayOrdersToolStripMenuItem_Click(object sender, EventArgs e)
{
using (NorthWindDataContext db = new NorthWindDataContext())
{
if (db.Employees == null)
{
}
EmployeeData currentEmployee = (EmployeeData)dataGridView1.SelectedRows[0].DataBoundItem;
var query =
from o in db.Employees
where o.EmployeeID == currentEmployee.EmployeeID
select new
{
orderNumber = o.Orders.Count()
};
dataGridView2.AutoGenerateColumns = false;
dataGridView2.DataSource = query;
}
类似的东西
where o.EmployeeID == currentEmployee.EmployeeID && o.Orders.Where(x=>x.OrderDate.Value.Date>FromDate.Value.Date&&<ToDate.Value.Date)
可以同时对DataGridViewColumn
和DataGridViewCell
类进行子类化,以托管您选择的任何控件。
MSDN上的这篇文章更详细地解释了这个过程,甚至包括一些示例代码:
如何:Windows窗体中的主机控件DataGridView单元格
您还可以在代码项目中找到一个完整的示例:Generic DataGridView V2.0