dataTable.Rowfilter总是返回第一行

时间:2014-01-02 00:33:08

标签: c# datagridview datatable

我有2个数据网格视图,显示两个来源应该是相同的数据。我测试差异,并希望应用过滤器仅显示存在差异的那些行。我为每个人创建一个DataView。从下面的代码中,您将看到一个DGV使用绑定源(dancesbindingsource)设置,另一个DGV直接将DataView设置为DataSource。我测试差异,然后设置每个的“view_flag”数据列,然后设置过滤器 - 在一个上使用BindingSource.filter,在另一个上使用DataView.RowFilter。

这一切都很好,除了在DataView的情况下,无论view_flag是打开还是关闭,DataTable的第一行每次都会显示。 BindingSource过滤器工作正常。我无法理解为什么。

6个月左右之前似乎发布了与dataTable.defaultView.RowFilter有关的类似问题,但建议的解决方案似乎就是我实际在做的并且得到了相同的现象。

任何建议都将不胜感激。

private void CheckDanceDataDifferences()
    {
        // Look at common ids in both databases and all data values
        // Start by creating a couple of useful data views
        DataView DOview = new DataView(dotable, "", "DanceID", DataViewRowState.CurrentRows);
        DataView DMview = new DataView(dmrecords.DMdanceTable, "", "id", DataViewRowState.CurrentRows);
        dancesBindingSource.DataSource = DOview;
        DMdgv.DataSource = DMview;
        DataGridViewRowCollection DOrows = dancesDataGridView.Rows;
        DataGridViewRowCollection DMrows = DMdgv.Rows;
        List<int> err_ids = new List<int>();
        foreach (DataGridViewRow row in DOrows) {
            int idmrow = DMview.Find(row.Cells["DanceID"].Value);
            if(idmrow >= 0) {
                bool[] err = CompareDataDifferences(row, DMrows[idmrow]);
                if(err != null) { 
                    err_ids.Add(idmrow);
                }
            }
        }

        if (err_ids.Count == 0) {
            MessageBox.Show("No data differences found");
            return;
        }

        int i, j, irow, nerr = err_ids.Count;
        // Note - all view flags have been initialised to 0
        // Now we set set the flags of those we want to see
        for (i = 0; i < nerr; i++) {
            irow = err_ids[i];
            DOrows[irow].Cells["view_flag"].Value = 1;
            DMrows[irow].Cells["view_flag"].Value = 1;
        }
        dancesBindingSource.Filter = "view_flag=1"
        DMview.RowFilter = "view_flag=1";
    }

1 个答案:

答案 0 :(得分:0)

您需要添加EndEdit方法以结束编辑过程。请尝试此代码。

// Note - all view flags have been initialised to 0
// Now we set set the flags of those we want to see
for (i = 0; i < nerr; i++)
{
    irow = err_ids[i];
    DOrows[irow].Cells["view_flag"].Value = 1;
    DMrows[irow].Cells["view_flag"].Value = 1;
    DMview[irow].EndEdit();
}
dancesBindingSource.EndEdit();
dancesBindingSource.Filter = "view_flag=1";
DMview.RowFilter = "view_flag=1";