【问题标题】:Change the style of particular cells of DataGridView in WinForms在 WinForms 中更改 DataGridView 特定单元格的样式
【发布时间】: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


【解决方案1】:
猜你喜欢
  • 2010-10-15
  • 2011-07-03
  • 1970-01-01
  • 2012-08-04
  • 2011-12-25
  • 1970-01-01
  • 2013-08-20
  • 2010-11-02
  • 1970-01-01
相关资源
最近更新 更多