过滤一个数据视图并保持与另一个数据视图的关系

本文关键字:数据 视图 另一个 关系 一个 过滤 | 更新日期: 2023-09-27 18:15:33

我得到两个datasets和两个datatables。绑定并显示在两个datagrids上:

//fill datatable
companiesDataTable = new DataTable("Companies");
scheduleDataTable = new DataTable("Schedule");
adapterCompanies.Fill(companiesDataTable);
adapterSchedule.Fill(scheduleDataTable);
//set dataset
myDataSet = new DataSet();
myDataSet.Tables.Add(companiesDataTable);
myDataSet.Tables.Add(scheduleDataTable);
//add relation
myDataSet.Relations.Add("FullSchedule",
                        myDataSet.Tables["Companies"].Columns["id"],
                        myDataSet.Tables["Schedule"].Columns["CompanyID"]);
//set binding sources
bsComp = new BindingSource(myDataSet, "Companies");
bsSched = new BindingSource(bsComp, "FullSchedule");
//fill datagrid
dataGridViewCompanies.DataSource = bsComp;
dataGridViewTimes.DataSource = bsSched;

如何过滤公司datagrid而不失去与datagrid的关系?

我试过了,但是关系似乎丢失了:

var view = myDataSet.Tables["Companies"].DefaultView;
view.RowFilter = filterString;                
dataGridViewCompanies.DataSource = view;

过滤一个数据视图并保持与另一个数据视图的关系

好吧,这很简单:

            bsComp.Filter = filterString;
            dataGridViewCompanies.DataSource = bsComp;