1

我希望找到一种方法将我的交替行模式应用于单个 datagridview 列。

我有一个使用 vb.net 的 Windows 窗体应用程序。现在我有一个模式,可以将每个其他 datagridview 单元格的背景颜色更改为不同的颜色。我的图案是白色然后是浅蓝色。我在下面包含了一张图片和我的代码。此代码将此模式应用于整个 datagridview,但我只想将其应用于一个,例如第二列索引。

           With Me.DataGridView1
                .DefaultCellStyle.BackColor = Color.White
                .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
            End With

在此处输入图像描述

4

2 回答 2

1

您可以将 if 语句添加到 datagridview 的 cellformatting 事件中,如下所示:

Private Sub DataGridView1_ConditionalFormatting_StatusCell(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    If e.ColumnIndex = 2 Then
        With Me.DataGridView1
            If e.RowIndex Mod 2 = 0 Then
                e.CellStyle.ForeColor = Color.White
            Else
                e.CellStyle.ForeColor = Color.AliceBlue
            End If
        End With
    End If
End Sub
于 2013-11-02T15:35:24.400 回答
0

如果您想为网格中的某些列提供替代样式,请使用这样的单元格格式化事件

Private Sub TDBGrid1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles TDBGrid1.CellFormatting
    With TDBGrid1
        If Not e.RowIndex Mod 2 = 0 AndAlso e.ColumnIndex > 3 Then
            e.CellStyle.BackColor = Color.Cyan
        Else
            e.CellStyle.BackColor = Color.White
        End If
    End With
    End Sub

在这里,我想要大于 3 的列索引的替代样式

于 2016-02-24T13:50:43.450 回答