4

我在使用DataGridView和DataGridViewComboBoxColumn讓用戶從圖像列表中選擇圖像時遇到問題。在標題爲「DatagridViewComboBoxColumn的自定義繪製」ref Link的問題中進行討論。我也遇到了這個問題,因爲只有當單元格處於編輯模式時纔會繪製圖像。當我點擊組合框外的某個地方時,所選圖像將消失!我已經實現了CellPainting事件來重繪圖像,但仍然無法解決問題。我測試DataGridViewComboBoxColumn用以下代碼:用DataGridViewComboBoxColumn顯示DataGridView中的選定圖像?

public Form1() 
    { 
     InitializeComponent(); 
     ..... 
     imageList.Images.Add(Properties.Resources.icon_priority_low); 
     imageList.Images.Add(Properties.Resources.icon_priority_medium); 
     ..... 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     .....    
     DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell)newDataGridView1.Rows[0].Cells[1]; 
     dgvcbc.Items.Add("test0"); 
     dgvcbc.Items.Add("test1"); 
     .....   
    } 

    private void newDataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     if (e.Control is ComboBox) 
     { 
      ComboBox theCB = (ComboBox)e.Control; 
      theCB.DrawMode = DrawMode.OwnerDrawFixed; 
      try 
      { 
       theCB.DrawItem -= combobox1_DrawItem; 
      } 
      catch { } 
      theCB.DrawItem += combobox1_DrawItem; 
     } 
    } 

    private void combobox1_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     Graphics g = e.Graphics; 
     Brush br = SystemBrushes.WindowText; 
     Brush brBack; 
     Rectangle rDraw; 
     bool bSelected = e.State == DrawItemState.Selected; 
     bool bValue = e.State == DrawItemState.ComboBoxEdit; 

     if ((e.Index < 0) || (columnIndex != 1)) 
      return; 

     rDraw = e.Bounds; 
     rDraw.Inflate(-1, -1); 

     int x, y; 

     x = e.Bounds.Left + 25; 
     y = e.Bounds.Top + 1; 
     int midX = (int)(e.Bounds.Width/2) + e.Bounds.Left; 

     // Show image and ignore text. 
     g.DrawImage(imageList.Images[e.Index], new Rectangle(midX - 6, y + 2, 12, 12));      
    } 

    private void newDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
    { 
     if (columnIndex != 1) 
      return; 

     Graphics g = e.Graphics; 
     Rectangle rDraw = newDataGridView1.GetCellDisplayRectangle(columnIndex, rowIndex, true); 

     e.PaintBackground(e.ClipBounds, true); 
     e.PaintContent(e.ClipBounds); 

     using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor)) 
     { 
      int y = rDraw.Y + 1; 
      int midX = (int)(rDraw.Width/2) + rDraw.X; 

      g.DrawImage(imageList.Images[0], new Rectangle(midX - 6, y + 2, 12, 12)); 

      e.PaintContent(e.ClipBounds); 
      e.Handled = true; 
     } 
    } 
} 

細胞將顯示,而不是圖片[0]「TEST0」如果我點擊的DataGridView的其它細胞。請你幫忙解決這個問題。非常感謝。

回答

0

最後致電PaintContent()會擦除您繪製的圖像。

繪製圖像之前,您必須繪製單元格(但不包括前景)。它看起來像這樣:

private void newDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    if (columnIndex != 1) 
     return; 

    Graphics g = e.Graphics; 
    Rectangle rDraw = newDataGridView1.GetCellDisplayRectangle(columnIndex, rowIndex, true); 

    e.Paint(e.CellBounds, e.PaintParts & ~DataGridViewPaintParts.ContentForeground); 

    int y = rDraw.Y + 1; 
    int midX = (int)(rDraw.Width/2) + rDraw.X; 

    g.DrawImage(imageList.Images[0], new Rectangle(midX - 6, y + 2, 12, 12)); 

    e.Handled = true; 
} 
相關問題