当我加载DGV时,我根据我的列(7)分配不同的单元格颜色。这完全加载。问题是,当我点击任何标题对列进行排序时,我的所有背景颜色都会回到"没有。"为什么? 我所做的只是点击标题来对列进行排序,而且我没有任何代码可以在点击单元格时更改背景颜色。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
daM = New OleDbDataAdapter("SELECT * FROM Kim_Items_Result", con)
Try
con.Open()
daM.Fill(dtM)
con.Close()
dgvMain.DataSource = dtM
With dgvMain
.Columns("Item").HeaderText = "Part #"
.Columns("Item").Width = 150
.Columns("ItemDescription").Width = 600
.Columns("PrimaryVendorNumber").Width = 400
.Columns("PrimaryVendorNumber").HeaderText = "Vendor"
.ClearSelection()
End With
Catch ex As Exception
con.Close()
MsgBox(ex.Message)
End Try
Dim col, col2 As New DataGridViewTextBoxColumn
'col.Visible = False
dgvMain.Columns.Add(col)
dgvMain.Columns.Add(col2)
For i As Integer = 0 To dgvMain.RowCount - 1
If dgvMain.Rows(i).Cells("ItemDescription").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("ItemDescription").Style.BackColor = Color.LightGreen
End If
If dgvMain.Rows(i).Cells("Min").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("Min").Style.BackColor = Color.GreenYellow
End If
If dgvMain.Rows(i).Cells("Max").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("Max").Style.BackColor = Color.OrangeRed
End If
If dgvMain.Rows(i).Cells("PrimaryVendorNumber").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("PrimaryVendorNumber").Style.BackColor = Color.DarkOrange
End If
If dgvMain.Rows(i).Cells("StdUM").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("StdUM").Style.BackColor = Color.PeachPuff
End If
If dgvMain.Rows(i).Cells("UMConversion").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("UMConversion").Style.BackColor = Color.DarkSalmon
End If
Next
For i As Integer = 0 To dgvMain.RowCount - 1
If dgvMain.Rows(i).Cells(0).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(1).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(2).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(3).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(4).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(5).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(6).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
Next
dgvMain.ClearSelection()
lblTotal.Text = dgvMain.RowCount
End Sub
答案 0 :(得分:1)
非常好用!谢谢JohnG。 我创建了一个CellEndEdit事件来更新单元格以及背景颜色。
Private Sub ColorCode()
For i As Integer = 0 To dgvMain.RowCount - 1
If dgvMain.Rows(i).Cells("ItemDescription").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("ItemDescription").Style.BackColor = Color.LightGreen
End If
If dgvMain.Rows(i).Cells("Min").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("Min").Style.BackColor = Color.GreenYellow
End If
If dgvMain.Rows(i).Cells("Max").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("Max").Style.BackColor = Color.OrangeRed
End If
If dgvMain.Rows(i).Cells("PrimaryVendorNumber").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("PrimaryVendorNumber").Style.BackColor = Color.DarkOrange
End If
If dgvMain.Rows(i).Cells("StdUM").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("StdUM").Style.BackColor = Color.PeachPuff
End If
If dgvMain.Rows(i).Cells("UMConversion").Value.ToString = Nothing Then
dgvMain.Rows(i).Cells("UMConversion").Style.BackColor = Color.DarkSalmon
End If
If dgvMain.Rows(i).Cells(0).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(1).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(2).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(3).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(4).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(5).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
If dgvMain.Rows(i).Cells(6).Value.ToString = Nothing Then dgvMain.Rows(i).Cells(8).Value = 1
Next
End Sub
Private Sub dgvMain_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMain.CellEndEdit
If dgvMain.CurrentCell.Value.ToString = Nothing Then Exit Sub
If dgvMain.CurrentCell.Value.ToString = dgvMain.CurrentRow.Cells(7).Value.ToString Then
dgvMain.CurrentRow.Cells(7).Value = Nothing
Else
dgvMain.CurrentRow.Cells(7).Value = dgvMain.CurrentCell.Value
ColorCode()
End If
End Sub