2

DataGridViewDataGridViewComboBoxColumn이 있습니다.사용자 지정 그림을 사용하는 DataGridViewComboBoxCell 자동 크기 조정

private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    if (e.ColumnIndex == 1 && e.RowIndex >= 0) 
    { 
     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 = Rectangle.FromLTRB(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Right, e.CellBounds.Bottom - 1); 

     brBack = Brushes.White; 
     Pen penGridlines = new Pen(dataGridView.GridColor); 
     g.DrawRectangle(penGridlines, rDraw); 
     g.FillRectangle(brBack, rDraw); 
     penGridlines.Dispose(); 

     if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null) 
     { 
      ComboboxColourItem oColourItem = (ComboboxColourItem)dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; 
      s = oColourItem.ToString(); 
      c = oColourItem.Value; 
     } 

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


     if (c != Color.Empty) 
     { 
      SolidBrush b = new SolidBrush(c); 
      Rectangle r = new Rectangle(e.CellBounds.Left + 6, 
             e.CellBounds.Top + 5, 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 + 3); 

      b.Dispose(); 
     } 

     e.Handled = true; 
    } 
} 

I에 의해 내 인구가 열을 자동 크기로 이동 더블 클릭 이것을 DVG의 오른쪽 편집 일어나는 것이다 : 이것은 내 사용자 지정 셀 그림 핸들러

Autosize

어떻게합니까 자동 크기 조정시 콤보 드롭 다운을 고려하여 동작을 조정하십시오.

감사합니다.

+0

그 페인트의 외부에 알려진이 드롭 다운 버튼의 폭 : 당신이 사용자 정의 셀을 만들하기로 결정하면, 당신은 유용 문제와 관련이 방법을 찾을 것입니다 행사? – DonBoitnott

+0

@DonBoitnott 아니요, 폭은 페인트 이벤트에서 'e.CellBounds.Height'로 지정되어 있으므로 어쨌든 사용하고 있을까요? –

+0

DGV를 사용하지 않기 때문에 어떤 이벤트가 있는지 알지 못하지만 순서대로 찾고 싶습니다. 열 자동 크기를 직접 무시합니다. 크기 조정 후에 발생하는 것; 또는'ColumnWidthChanged'. 그 중 하나를 사용하여 관심있는 열인지 알아 내고 단추의 너비를 조금 더 추가 할 수 있어야합니다. 자동 크기 조정 기능은 절대로 알지 못하므로 수동으로 수행해야합니다. – DonBoitnott

답변

2

열 머리글 구분 기호를 두 번 클릭하여 열 자동 크기를 만들면 열 너비가 가장 넓은 셀과 같습니다. 자동 크기 모드에서 콤보 상자 셀의 폭이 계산됩니다 : 서식 값의

  • 콤보 상자 버튼
  • 셀 스타일 패딩
  • 오류 아이콘 폭
  • 약간 통통함의 폭 텍스트와 버튼 및 아이콘 주위

빠른 수정

색이있는 사각형이 공간을 차지하지만 셀의 자동 크기를 계산할 때 주위의 너비와 공백이 계산되지 않습니다.

간단한 해결책으로 열에 약간의 여백을 추가 할 수 있습니다. 디자이너에서 열을 편집하고 열의 패딩을 DefaultCellStyle에서 설정할 수 있습니다. 또한 예를 들어 쓸 수있는 코드를 사용하여 :

column.DefaultCellStyle.Padding = new Padding(16,0,16,0); 

장기 솔루션

열이 재사용 가능한 열 유형 인 경우, 당신이 사용자 정의 열 유형을 작성하는 것이 좋습니다. 그런 다음 페인팅 로직을 캡슐화하고 사용자 정의 셀의 크기 및 기타 기능을 계산할 수 있습니다.

+0

고마워요! 훌륭하게 작동합니다. –

+0

당신은 환영합니다 :) –

+1

put'ComboBoxRenderer.DrawDropDownButton ...드로잉이 끝나면 버튼을 텍스트 위에 렌더링해야합니다. 현재 보시다시피 텍스트는 콤보 상자 위에 그려져 있습니다. 텍스트를 그린 후에는 콤보 상자 단추를 그려야합니다. –