【问题标题】:DataView RowFilter removes entire rowDataView RowFilter 删除整行
【发布时间】:2014-01-24 13:33:57
【问题描述】:

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

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

我每隔一秒刷新一次 DataTable,每次我必须确定行是否有效。现在它起作用了。如果 Valid 属性设置为 false,则行从 DGW 中消失。

我的表有 6 行(并且总是会有)。我想要实现的是,如果有效为假,我不想从 DGV 中消失整行。我只想从单元格中删除值以使其看起来为空。

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

如果在表中添加了新行,我不想在 DGW 中添加新行,我想填充 emtpy 行。如果从表中删除了某行,我不想将其从 DGW 中删除,我只想清理该行中的单元格...

有没有办法通过 DataView 实现这一点,或者我必须用我的代码手动检查它(并在 DataView 中禁用 RowFilter)?谢谢

【问题讨论】:

    标签: vb.net datagridview datatable dataview rowfilter


    【解决方案1】:

    过滤器对此不起作用。您将需要在网格本身中处理它。这是一种可能对您有所帮助的方法

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多