【问题标题】:Change datagridview cell background based on a external parameter根据外部参数更改 datagridview 单元格背景
【发布时间】:2014-07-18 12:53:32
【问题描述】:

我应该将颜色更改为包含参数“tarjeta_fam”的单元格。我尝试更改单元格默认属性,然后使该行无效以刷新它,但(显然)没有任何反应。可以从单元格格式化事件中更改单元格颜色吗?

Public Sub New(user As Usuario, ByVal tarjeta_fam As String)
  InitializeComponent()

  gridFamiliares.DataSource = BD.getTable(a query)

  If Me.gridFamiliares.Rows.Count > 0 Then
    For i As Integer = 0 To Me.gridFamiliares.Rows.Count - 1
      If Me.gridFamiliares.Rows(i).Cells("tarjeta_fam").Value = tarjeta_fam Then
         Me.gridFamiliares.Rows(i).DefaultCellStyle.BackColor = Color.Black
         Me.gridFamiliares.InvalidateRow(i)
      End If
    Next
    End If
End Sub

【问题讨论】:

标签: vb.net gridview datagridview cell


【解决方案1】:

DataGridView 控件确实希望您为此使用 CellFormatting 事件,因此声明该事件要使用的表单级变量:

Private tarjeta_fam_Value As String = String.Empty

Public Sub New(user As Usuario, ByVal tarjeta_fam As String)
  InitializeComponent()
  gridFamiliares.DataSource = BD.getTable(a query)
  tarjeta_fam_Value = tarjeta_fam
End Sub

Private Sub gridFamiliares_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles gridFamiliares.CellFormatting
  If tarjeta_fam_Value <> String.Empty Then
    With gridFamiliares.Rows(e.RowIndex)
      If .Cells("tarjeta_fam").Value = tarjeta_fam_Value Then
        .DefaultCellStyle.BackColor = Color.Black
      End If
    End With
  End If
End Sub

【讨论】:

    猜你喜欢
    • 2013-04-12
    • 2016-05-27
    • 2012-01-29
    • 2013-11-23
    • 2023-03-26
    • 2011-04-03
    • 2015-07-11
    • 2020-04-08
    • 2021-02-23
    相关资源
    最近更新 更多