如何将日期选择器与datagridview集成?

时间:2013-02-17 09:50:06

标签: c# sql gridview

在我的应用中,我正在对员工进行查询,以了解每个员工的订单数量。 我想添加一个日期过滤器。所以我创建了两个日期时间选择器并为它们构建了一个函数。哪里&我应该如何在它们之间进行整合,或者我可以在查询中输入一些如何?

代码:

   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)

1 个答案:

答案 0 :(得分:1)

可以将DataGridViewColumnDataGridViewCell类子类化为托管您选择的任何控件。

MSDN上的这篇文章更详细地解释了这个过程,甚至还包括一些示例代码:
How to: Host Controls in Windows Forms DataGridView Cells

您还可以在代码项目中找到完整的示例:Generic DataGridView V2.0