2014-01-24 54 views
0

我有一个DataTable几列。最后一列是布尔类型,它表示该行是否有效(应/不应显示在DataGridView中)。接下来我有被设置为DataSoure到DataGridView的一个DataView:DataView RowFilter删除整行

MyDataView = New DataView(MyDataTable, "Valid = True", Nothing, DataViewRowState.CurrentRows) 
MyDataGridView.DataSource = MyDataView 

我在一周秒钟的时间间隔清爽的DataTable,每次我必须决定是否行是有效的。现在它可以工作。如果有效属性设置为false,则行从DGW中消失。

我的表格有6行(并且总会有)。我想实现的是,如果有效是错误的,我不想从DGV中消失整行。我只想从单元格中删除值以将其视为空白。

我的观点是,如果表格中的行为空,DGF包含所有的行事件(您可以在DGW中看到行但为空)。每行有不同的背景颜色(偶数行是白色的,奇数行是灰色的)。

如果一个新行被添加到表中,我不想在DGW中添加新行,我想填充emtpy行。如果从表中删除了一些行,我不想将它从DGW中删除,我只希望在这一行中清除单元格...

有没有办法如何使用DataView来实现此目标,或者我必须用我的代码手动检查它(并在DataView中禁用RowFilter)?谢谢

回答

0

该过滤器将无法正常工作。您需要在网格中处理它。这里有一个方法可以帮助你

Private Sub grid_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles grid.CellPainting 

    If e.RowIndex > -1 AndAlso e.ColumnIndex > -1 Then 
     Dim oVw As DataRowView 
     oVw = TryCast(grid.Rows(e.RowIndex).DataBoundItem, DataRowView) 
     If Not oVw Is Nothing AndAlso CBool(oVw.Item("Valid")) Then 
      e.Handled = True 
      e.PaintBackground(e.ClipBounds, True) 
     End If 
    End If 
End Sub