设置所有行的行背景色后,更改单个列的背景色 [英] Change a backcolor of a single column after a Row Backcolor for all rows is set
问题描述
我在datagrid视图中有10行,我使用VS2010.
设置所有行背景色后,如何使单列背景色?
我在表单加载事件上编写代码.我的代码在vb.net(Windows窗体)中,如下所示用于行颜色:
I have 10 rows in datagrid view and I use VS2010.
How can I make a single column backcolor after all rows backcolor is set?
I write my code on form load event. My code is in vb.net(windows form) as follows for the rows color:
Dim CountR As Integer
CountR = 0
While CountR < DataGridView1.RowCount
If CountR Mod 2 = 0 Then
DataGridView2.Rows(CountR).DefaultCellStyle.BackColor = Color.Pink
Else
DataGridView2.Rows(CountR).DefaultCellStyle.BackColor = Color.SkyBlue
End If
CountR = CountR + 1
End While
推荐答案
首先,我将使用:
For Each row As DataGridViewRow In DataGridView2.Rows
If row.Index Mod 2 = 0 Then
row.DefaultCellStyle.BackColor = Color.Pink
Else
row.DefaultCellStyle.BackColor = Color.SkyBlue
End If
Next
然后,由于您已经使用Rows.DefaultCellStyle设置了背景色,因此不能简单地使用Columns.DefaultCellStyle.您将不得不再次使用Rows.DefaultCellStyle.我不知道您想如何筹办这项活动,不过例如在DGV和Column选择上使用ContextMenuStrip,您可以使用:
Then, since you''ve set the backcolor using the Rows.DefaultCellStyle you cannot simply use the Columns.DefaultCellStyle. You''ll have to use the Rows.DefaultCellStyle again. I don''t know how you want to raise the event but e.g. with a ContextMenuStrip on the DGV and Column selection, you can use:
Public Sub HighlightColumnToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HighlightColumnToolStripMenuItem.Click
For Each col As DataGridViewColumn In DataGridView2.SelectedColumns
For Each row As DataGridViewRow In DataGridView2.Rows
row.Cells(col.Index).Style.BackColor = Color.Purple
Next
Next
End Sub
这篇关于设置所有行的行背景色后,更改单个列的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!