【发布时间】: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