如何将gridview绑定到另一个网格视图?

时间:2013-02-17 06:27:45

标签: c# mysql sql gridview

我在我的项目中使用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;

        }
}

请帮忙

由于

1 个答案:

答案 0 :(得分: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;

    }
}