过滤一个datagridview并保持与另一个的关系

时间:2014-07-29 17:05:58

标签: c# winforms datagridview dataset

我有两个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;

1 个答案:

答案 0 :(得分:0)

好吧,这简直令人尴尬:

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