【问题标题】:DataGridView only refreshing styles upon cell clickDataGridView 仅在单击单元格时刷新样式
【发布时间】:2013-01-13 15:47:39
【问题描述】:

我在 VB.NET 和 VS2012 中有一个 DGV。我正在尝试动态更改各种单元格的单元格格式。以下是我的代码:

Private Sub gridFinancial_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles gridFinancial.CellFormatting
    Try
        For Each row As chgltrDataSet.gridsourceRow In frmFinBatchChrg.ChgltrDataSet.gridsource.Rows
            If gridFinancial.CurrentRow.Cells("CompBool").Value = True Then
                Me.gridFinancial.CurrentRow.Cells(0).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(1).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(2).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(3).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(4).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(5).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(6).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(7).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(8).Style.BackColor = Color.Yellow

                Me.gridFinancial.CurrentRow.Cells(0).ReadOnly = True
                Me.gridFinancial.CurrentRow.Cells(1).ReadOnly = True
                Me.gridFinancial.CurrentRow.Cells(2).ReadOnly = True
                Me.gridFinancial.CurrentRow.Cells(3).ReadOnly = True
                Me.gridFinancial.CurrentRow.Cells(4).ReadOnly = True
                Me.gridFinancial.CurrentRow.Cells(5).ReadOnly = True
                Me.gridFinancial.CurrentRow.Cells(6).ReadOnly = True
                Me.gridFinancial.Update()
                Me.gridFinancial.Refresh()
    End if

    Catch ex As Exception

    End Try


End Sub

我已阅读此内容:http://msdn.microsoft.com/en-us/library/1yef90x0.aspx,也许我遗漏了一些东西,但现在,应用该代码后,如果我在绘制 DataGridView 后单击受影响的单元格之一,我的 DataGridView 只会反映该代码。换句话说,在加载 DataGridView 之后,单元格只有在我单击它们后才会显示为黄色(然后该行中应该为黄色的所有单元格都显示为黄色)。为什么是这样?我不确定我做错了什么。

作为一个附带问题,在我的 DGV 绘制之前,此单元格格式化事件至少会触发 40-50 次,而且它只是一个 6 行数据源。没有更好的事件触发器吗?我确信我的代码可能会更好,但这似乎效率很低。

以上代码中的只读属性工作正常,所以我知道事件触发正确。

【问题讨论】:

    标签: vb.net datagridview formatting cell


    【解决方案1】:

    您可以通过DataGridViewCellFormattingEventArgs 访问该样式,而不是像您一样设置单元格样式。

    所以是这样的:

    e.CellStyle.BackColor = Color.Yellow
    

    要考虑的另一件事是,通常最好在 DataBindingComplete 事件中附加您的 CellFormatting 处理程序。

    Private Sub DataGridView1_DataBindingComplete(sender As System.Object, e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
        AddHandler DataGridView1.CellFormatting, AddressOf Me.DataGridView1_CellFormatting
    End Sub
    

    这解决了在渲染 DataGridView 的可视部分时的一些奇怪行为,方法是确保在附加后续事件之前完成所有操作。


    这个带有 DataBindingComplete 事件的技巧至少应该减少对 CellFormatting 的不必要调用 - 每次更新单元格时都会触发单元格格式化。一旦你全部启动并运行,这只是用户离开单元格的一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-04
      • 2011-12-25
      • 2016-04-21
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      相关资源
      最近更新 更多