2013-07-12 18 views
0

我有三個datagridviews(Department,Employee,EmployeeNotInDepartment)。我已經根據DataRelations填充了Department和Employee datagridviews(請參閱下文)。我在想,必須有一個明顯簡單的方法來填充EmployeeNotInDepartment的datagridview。有任何想法嗎?我希望我不必使用linq。數據表DataRelation右對外加入

public Form1() 
{ 
     InitializeComponent(); 

     dtDepartment = FillDepartmentList(); 
     dtEmployee = FillEmployeeList(); 
     dsDepartmentEmployees = new DataSet(); 

     // Add tables to dataset 
     dsDepartmentEmployees.Tables.Add(dtDepartment); 
     dsDepartmentEmployees.Tables.Add(dtEmployee); 

     // Create table relationship 
     dsDepartmentEmployees.Relations.Add("DepartEmpRelation", dtDepartment.Columns["DepartmentNumber"], dtEmployee.Columns["DepartmentNumber"],true); 

     BindingSource bsDepartment = new BindingSource(); 
     bsDepartment.DataSource = dsDepartmentEmployees; 
     bsDepartment.DataMember = "table1"; 

     BindingSource bsEmployee = new BindingSource(); 
     bsEmployee.DataSource = bsDepartment; 
     bsEmployee.DataMember = "DepartEmpRelation"; 

     dataGridView1.DataSource = bsDepartment; 
     dataGridView2.DataSource = bsEmployee; 

} 
+0

任何具體的理由不使用linq?它管理你的所有關係。 – AlwaysAProgrammer

+0

我知道如何使用linq做到這一點,但我想知道是否有一種明顯簡單的方法來使用數據集關係來完成它。我找不到這種方法的任何文檔。 –

回答

0

如果你堅持不使用LINQ,那麼你可以使用DataView並在RowFilter屬性指定的過濾器(它可能是一個逗號分隔的ID列表,你不希望顯示)

This is how使用RowFilterDataView

+0

感謝幫助,我想我現在可以使用linq。 –

0

正如@Rwiti建議,您可以使用DataView的RowFilter屬性,您可以定義適當的表達式來過濾出基於該行的行。

看看下面的鏈接,列出的RowFilter的各種例子包括NOT IN

http://www.csharp-examples.net/dataview-rowfilter/

但是,如果你沒有任何理由不使用LINQ(即與.NET 2.0一起),然後去吧。