vb.net 如何仅将交替行模式应用于一个 datagridview 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19743429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:42:03  来源:igfitidea点击:

How to apply alternating row pattern to only one datagridview column

vb.netdatagridview

提问by stackexchange12

I'm hoping to find a way to apply my alternating row pattern to a single datagridview column.

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

I have a windows forms application using vb.net. Right Now I have a pattern that changes the backcolor of every other datagridview cell to a different color. My pattern is white then light blue. I've included an image below and my code. This code applies this pattern to the entire datagridview, but I only want to apply it to one, for instance the second column index.

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

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

enter image description here

在此处输入图片说明

回答by majjam

You can add an if statement to your datagridview's cellformatting event like this:

您可以将 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

回答by Prashant Datir

If you want to give alternate style to some column from grid use cellformating event like this

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

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

Here i wanted alternate style to column index which is greater than 3

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