开发者

How do I open a Windows Form returning only data from specific id

开发者 https://www.devze.com 2022-12-22 17:49 出处:网络
I have a C# windows application which does the following: 1) the main form is called EmployeeForm.cs which holds 1 employee record from a sql database. I have used linq to sql to display the records.

I have a C# windows application which does the following:

1) the main form is called EmployeeForm.cs which holds 1 employee record from a sql database. I have used linq to sql to display the records.

2) There is a button on the EmployeeForm when clicked opens another form called Orders.cs which displays a datagrid of orders pertaining to the Employee ID. I am using linq to sql again to display this data.

3) I have got everything working except being able to filter the datagrid by the specific Employee ID. I assume I need some sort of where clause in the orders linq statement??

I know how to achieve this a few different ways for the web but cannot work this out for my windows app. I have included what I have so far:

public partial class EmployeeForm : Form
{
    private NorthWindDataContext db;
    public EmployeeForm()
    {
        InitializeComponent();
        db = new NorthWindDataContext();
        var employeeQuery = from employee in db.Employees
                            orderby employee.FirstName
                            select employee;
        employeeBindingSource.DataSource = employeeQuery;
    }

    private void Orders_Click(object sender, EventArgs e)
    {
        OpenOrdersForm();                         
    }

    private void OpenOrdersForm()
    {
        OrderForm orderFormInstance = new OrderForm();
        orderFormInstance.ShowDialog();
    }
}

an开发者_JAVA百科d

 public partial class OrderForm : Form

 {private NorthWindDataContext db;

    public OrderForm()
    {
        InitializeComponent();
    }

    private void OrderForm_Load(object sender, EventArgs e)
    {
        db = new NorthWindDataContext();
        var ordersQuery = from orders in db.Orders
                          orderby orders.OrderID
                          select orders;
        orderBindingSource.DataSource = ordersQuery;
    }

}


What have you tried so far?
Is this what you are looking for?

from orders in db.Orders
  where orders.employeeID == 42 // supposing orders has an employeeID field
  orderby orders.OrderID
  select orders;

Have a look at this MSDN page, under "filtering": Basic LINQ Query Operations (C#)

0

精彩评论

暂无评论...
验证码 换一张
取 消