0

2 gridview在我的项目中使用,我将员工绑定在一个网格上,我想在第二个网格上显示每个员工的订单。我一直在过滤其他员工,当我点击显示订单按钮时​​,它会显示所有订单,我如何指向正确的索引(选择的员工)?.

我的代码:

private void Form3_Load(object sender, EventArgs e)
{
        using (NorthWindDataContext db = new NorthWindDataContext())
        {

            var query =
                from d in db.Employees
                select new
        {
            d.FirstName
        };
            dataGridView1.DataSource = query;
        }

}  

private void displayOrdersToolStripMenuItem_Click(object sender, EventArgs e)
{
        using (NorthWindDataContext db = new NorthWindDataContext())
        {

            var query =
                from o in db.Orders
                select new
                {
                    o.ShipName <----problem  here :(
                };
            dataGridView2.DataSource = query;

        }
}

请帮忙

谢谢

4

1 回答 1

2

使用GridView.SelectedValue属性。

您需要编写类似于以下内容的内容:

private void displayOrdersToolStripMenuItem_Click(object sender, EventArgs e)
{
    using (NorthWindDataContext db = new NorthWindDataContext())
    {

        var query =
            from o in db.Orders.
                Where(item => item.ID == dataGridView1.SelectedValue)
            select new
            {
                o.ShipName <----problem  here :(
            };
        dataGridView2.DataSource = query;

    }
}
于 2013-02-17T06:32:11.993 回答