我在项目中使用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;
}
}
请帮忙
谢谢
最佳答案
使用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;
}
}