DataTables DataRelation与右外连接相反

时间:2013-07-12 16:34:53

标签: c# datarelation

我有三个datagridviews(Department,Employee,EmployeeNotInDepartment)。我已根据DataRelations填充了Department和Employee datagridviews(见下文)。我认为必须有一种显而易见的方法来填充EmployeeNotInDepartment数据网格视图。有任何想法吗?我希望我不必使用linq。

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;

}

2 个答案:

答案 0 :(得分:0)

如果您坚持不使用LINQ,那么您可以使用DataView并在RowFilter属性中指定您的过滤器(它可能是您不想显示的逗号分隔的ID列表)

This is howRowFilter

上使用DataView

答案 1 :(得分:0)

正如@Rwiti建议的那样,你可以使用DataView的RowFilter属性,你可以定义适当的表达式来根据它来过滤掉行。

查看以下链接,其中列出了RowFilter的各种示例,包括NOT IN

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

但如果您没有任何理由不使用Linq(即必须使用.net 2.0),那就去吧。