2017-02-23 167 views
0

我已經創建了從DataGridViewColumn繼承的自定義DataGridviewColumn。我正在繪製一個基於底層數據的圖標是真還是假。我無法確定如何確定底層行是否被選中,以便我可以相應地繪製單元格背景。我不成功試過如下:選擇自定義Datagridviewcolumn背景顏色

if (cellState == DataGridViewElementStates.Selected) 
       { 
        graphics.FillRectangle(new SolidBrush(cellStyle.SelectionBackColor), cellBounds); 
       } 
       else 
       { 
        graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds); 
       } 

唯一的其他選擇是引用父網格,並檢查基礎上,的rowIndex選中狀態。有沒有更好的方法,以及如何引用父網格?

+0

具有u試過這種http://stackoverflow.com/questions/2189376/how-to-change-row-color-in-datagridview –

+0

我創建了一個自定義列,我希望能夠繪製單元格時從繪畫事件中繪製背景顏色。我不想在每次使用自定義列時都處理行顏色等。 – AlMacOwl

回答

0

解決了我的問題。 DataGridViewCell公開可以爲所選行檢查的父網格。

if (this.DataGridView.Rows[rowIndex].Selected) 
       { 
        graphics.FillRectangle(new SolidBrush(cellStyle.SelectionBackColor), cellBounds); 
       } 
       else 
       { 
        graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds); 
       }