【发布时间】:2013-09-06 08:23:15
【问题描述】:
我在我的 Windows 应用程序中将 DataGridView 与 DataTable 绑定。在 DataGridView 的 RowsAdded 事件中,我正在编写代码以更改特定单元格的样式。
我已经尝试了以下代码,但它不起作用
Private Sub grdView_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles grdView.RowsAdded
Dim f = New Font("Times New Roman", 10, FontStyle.Bold)
For index As Integer = e.RowIndex To e.RowIndex + e.RowCount - 1
Dim row As DataGridViewRow = grdView.Rows(index)
If Decimal.Parse(row.Cells("Variance Up").Value.ToString) >= Decimal.Parse(txtRise1.Text) Then
row.Cells("Variance Up").Style.ForeColor = Color.Red
row.Cells("Variance Up").Style.Font = f
ElseIf Decimal.Parse(row.Cells("Variance Up").Value.ToString) >= Decimal.Parse(txtRise2.Text) Then
row.Cells("Variance Up").Style.ForeColor = Color.Red
row.Cells("Variance Up").Style.Font = f
End If
Next
End Sub
我想做的如下:
【问题讨论】:
-
怎么不工作?
it's not working太笼统了。顺便说一句,RowsAdded事件不适合,除非您的数据在添加后没有更改。 -
@KingKing 问题是每个网格绑定都会触发 RowsAdded 事件四次
-
@FarhanMukadam 这就是
RowsAdded不适合的部分原因。当然,当您绑定网格时,它会为您添加行(而不是手动执行)并且RowsAdded将被触发。 -
@KingKing 那么解决方案是什么?我应该使用哪个事件?
-
@FarhanMukadam 可能是
RowPosPaint或CellValueChanged
标签: .net vb.net winforms datagridview