2013-02-17 57 views
0

我在我的項目中使用2 gridview,我將員工綁定在一個網格上,我想在第二個網格上顯示每個員工的訂單。我被卡在過濾其他員工,當我點擊顯示訂單按鈕時​​,它顯示我所有的訂單,我如何指向正確的索引(員工選擇)? 。如何將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; 

     } 
} 

請幫

感謝

+0

什麼是錯誤的異常? – spajce 2013-02-17 06:55:07

回答

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; 

    } 
} 
+0

我不能看到的SelectedValue道具,我有選擇行,選擇單元格,選定的列和其他.. – darko 2013-02-17 06:44:29

+0

嘗試dataGridView1.SelectedItem.Cells [keyColumnIndex] .. – 2013-02-17 06:58:12

相關問題