2016-06-07 71 views
0

这里的想法是,我重绘了combol“cell”,以便显示颜色和文本块。这是表格显示时,它是要告诉下拉:我已经选择了颜色在DataGridView中使用CellPaint事件处理程序

Default rendering

后它怪异:

Weird results

现在是完全错误的。我必须将鼠标悬停在控件上以呈现其他位。只是不正​​确。

我的处理程序:

private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
    { 
     if(e.ColumnIndex == 0 && e.RowIndex > 0) 
     { 
      e.PaintBackground(e.ClipBounds, true); 
      e.PaintContent(e.ClipBounds); 

      Graphics g = e.Graphics; 
      Color c = Color.Empty; 
      string s = ""; 
      Brush br = SystemBrushes.WindowText; 
      Brush brBack; 
      Rectangle rDraw; 

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

      { 
       brBack = Brushes.White; 
       g.FillRectangle(brBack, e.ClipBounds); 
      } 

      try 
      { 
       ComboboxColorItem oColorItem = (ComboboxColorItem)((ComboBox)sender).SelectedItem; 
       s = oColorItem.ToString(); 
       c = oColorItem.Value; 
      } 
      catch 
      { 
       s = "red"; 
       c = Color.Red; 
      } 

      SolidBrush b = new SolidBrush(c); 
      Rectangle r = new Rectangle(e.ClipBounds.Left + 5, e.ClipBounds.Top + 3, 10, 10); 
      g.FillRectangle(b, r); 
      g.DrawRectangle(Pens.Black, r); 
      g.DrawString(s, Form.DefaultFont, Brushes.Black, e.ClipBounds.Left + 25, e.ClipBounds.Top + 1); 

      b.Dispose(); 
      g.Dispose(); 

      e.Handled = true; 
     } 
    } 
} 

有什么我失踪?一定是。

更新:

我在CellPainting事件试过这样:

if(e.ColumnIndex == 0 && e.RowIndex > 0) 
{ 
    using (Graphics g = e.Graphics) 
    { 
     g.FillRectangle(Brushes.Aqua, e.CellBounds); 
    } 

} 
else 
{ 
    e.PaintBackground(e.CellBounds, true); 
    e.PaintContent(e.CellBounds); 
} 
e.Handled = true; 

改善在这个意义上的东西,它不走怪异。当然,它实际上并没有画任何东西。但是,最后剩下的细胞(带有编辑符号)只能以白色显示。所以它的机制仍然是不正确的。 谢谢。

如果我尝试的方式建议我结束了:

Results

所取得的进展!我们是否可以让它继续包含网格线?像正常细胞一样?

Show gridlines

+1

绝对不使用ClipBounds。 – LarsTech

+0

@LarsTech,我从这里得到:http://stackoverflow.com/questions/7482534/custom-draw-of-datagridviewcomboboxcolumn –

+1

我将不得不重新回答我的答案。 :-) – LarsTech

回答

1

  • 交换所有ClipBounds通过CellBounds
  • 删除后,g.Dispose();

..things看起来几乎是正常的。

这是结果:

enter image description here

Paint事件:

private void dataGridView2_CellPainting(object sender, 
             DataGridViewCellPaintingEventArgs e) 
{ 
    if (e.ColumnIndex == 4 && e.RowIndex == 0) // use your own checks here!! 
    { 
     e.PaintBackground(e.CellBounds, true); 
     e.PaintContent(e.CellBounds); 

     Graphics g = e.Graphics; 
     Color c = Color.Empty; 
     string s = ""; 
     Brush br = SystemBrushes.WindowText; 
     Brush brBack; 
     Rectangle rDraw; 

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

     { 
      brBack = Brushes.White; 
      g.FillRectangle(brBack, rDraw); // ** 
     } 

     try 
     { // use your own code here again! 
      // ComboboxColorItem oColorItem = 
      //  (ComboboxColorItem)((ComboBox)sender).SelectedItem; 
      s = "WW";// oColorItem.ToString(); 
      c = Color.LawnGreen;// oColorItem.Value; 
     } catch 
     { 
      s = "red"; 
      c = Color.Red; 
     } 

     // asuming a square is right; make it a few pixels smaller! 
     int butSize = e.CellBounds.Height; 
     Rectangle rbut = new Rectangle(e.CellBounds.Right - butSize , 
             e.CellBounds.Top, butSize , butSize); 
     ComboBoxRenderer.DrawDropDownButton(e.Graphics, rbut, 
        System.Windows.Forms.VisualStyles.ComboBoxState.Normal); 


     SolidBrush b = new SolidBrush(c); 
     Rectangle r = new Rectangle(e.CellBounds.Left + 5, 
            e.CellBounds.Top + 3, 10, 10); 
     g.FillRectangle(b, r); 
     g.DrawRectangle(Pens.Black, r); 
     g.DrawString(s, Form.DefaultFont, Brushes.Black, 
        e.CellBounds.Left + 25, e.CellBounds.Top + 1); 

     b.Dispose(); 
     //g.Dispose(); <-- do not dispose of thing you have not created! 

     e.Handled = true; 
    } 

} 

请注意,我只有一个CombBoxCell,所以我改变了检查。而且我没有,所以我用一个随机字符串&替换了颜色。从OP

更新:我有一些语法错误,需要的:

// use your own code here again! 
if(DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null) 
{ 
    ComboboxColorItem oColorItem = (ComboboxColorItem)DataGridView1 
            .Rows[e.RowIndex].Cells[e.ColumnIndex].Value; 
    s = oColorItem.ToString(); 
    c = oColorItem.Value; 
} 
+0

组合框本身很好。我只是希望单元格(当不被编辑时)具有颜色和文本块下降箭头。 –

+0

我可以请看看你的版本,因为我的错误。 –

+0

感谢您提供更多信息。请看我更新的问题。我得到不同的结果。 –