2016-06-07 17 views
0

생각은 여기에 "셀"을 다시 그려 색과 텍스트 블록을 표시합니다. 이 때 폼을 표시이며 드롭 다운을 표시하는 것입니다 : 나는 색상을 선택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

삭제 모든 ClipBoundsCellBounds

  • 에 의해 교환

    • g.Dispose();

    ..things 거의 정상 본다. 이 Paint 이벤트의

    enter image description here

    :

    은 결과 난 단지 하나의 CombBoxCell

    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; 
        } 
    
    } 
    

    주, 그래서 검사를 변경했습니다. 그리고 나는 이 없으므로 임의의 문자열 &을 대체했습니다. 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

    더 많은 정보를 제공해 주셔서 감사합니다. 내 업데이트 된 질문을 참조하십시오. 나는 다른 결과를 얻는다. –