2017-09-22 84 views
0

當我加載我的DGV時,我根據列(7)分配不同的單元格背景顏色。這完美加載。問題是當我點擊任何標題對列進行排序時,我所有的背景顏色都會回到「無」。爲什麼? 我所做的只是單擊標題來對列進行排序,而我沒有任何代碼來單擊單元格時更改背景顏色。datagridview列標題問題

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

您必須在排序後再次重新分配顏色(標題點擊)。當用戶單擊標題時,排序機制正在將背景顏色更改回默認狀態。當條件滿足時,使用某些「單元格更改」事件機制可以更容易地將更改的單元格格式化爲適當的顏色。否則,仍然可以遍歷所有行並在網格排序或單元格值更改時設置顏色。選擇你的毒藥;-)。 – JohnG

+0

JohnG,你是男人!非常感謝,它完美的工作我使用「排序」事件重新分配背景顏色。 – JCLD

回答

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