2009-11-21 3 views
1

에 나는아이콘은 DataGridViewComboBoxColumn

DataGridViewComboBoxColumn TransferActionCol = new DataGridViewComboBoxColumn(); 
TransferActionCol.DataSource = Enum.GetValues(typeof(TransferActionEnum)); 
TransferActionCol.DataPropertyName = "TransferAction"; 
TransferActionCol.Name = "Transfer Action"; 
TransferActionCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; 
fileListdataGridView.Columns.Add(TransferActionCol); 

TransferActionEnum 값 다운로드, 업로드 및 무시에 열거입니다 다음과 같이 정의되어 내 응용 프로그램에서 DataGridViewComboBoxColumn 있습니다. 모든 것은 정상적으로 작동하지만 열거 형 텍스트 값 대신이 열의 셀에 아이콘을 표시하는 방법이 있는지 알고 싶습니다. 가능하다면 사용자가 선택을하고 나면 아이콘을 모두 표시하고 싶습니다.

+0

약 IValueConverter는 무엇입니까? –

답변

1

MSDN Answer이 작동합니까? 번역을 남겨주세요 :

EDIT : C# 버전은 원본 페이지에서 사용할 수 있습니다.

Private Sub Form1_Load(ByVal sender As System.Objec t, ByVal e As System.EventArgs) Handles MyBase.Load 
    Dim cboColumn As DataGridViewComboBoxColumn 
    cboColumn = New DataGridViewComboBoxColumn 
    With cboColumn 
     .Name = "Color" 
     .Items.Add("Red") 
     .Items.Add("Blue") 
     .Items.Add("Green") 
    End With 
    Me.DataGridView1.Columns.Add(cboColumn) 
    Dim txtColumn As DataGridViewTextBoxColumn 
    txtColumn = New DataGridViewTextBoxColumn 
    With txtColumn 
     .Name = "Description" 
    End With 
    Me.DataGridView1.Columns.Add(txtColumn) 
End Sub 

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing 
    If TypeOf e.Control Is ComboBox Then 
     DirectCast(e.Control, ComboBox).DrawMode = DrawMode.OwnerDrawFixed 
     Try 
      RemoveHandler DirectCast(e.Control, ComboBox).DrawItem, AddressOf combobox1_DrawItem 
     Catch ex As Exception 

     End Try 
     AddHandler DirectCast(e.Control, ComboBox).DrawItem, AddressOf combobox1_DrawItem 
    End If 
End Sub 

Private Sub combobox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) 
    Dim g As Graphics = e.Graphics 
    Dim s As String 
    Dim br As Brush = SystemBrushes.WindowText 
    Dim brBack As Brush 
    Dim rDraw As Rectangle 
    Dim bSelected As Boolean = CBool(e.State And DrawItemState.Selected) 
    Dim bValue As Boolean = CBool(e.State And DrawItemState.ComboBoxEdit) 

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

    If bSelected And Not bValue Then 
     brBack = Brushes.LightBlue 
     g.FillRectangle(Brushes.LightBlue, rDraw) 
     g.DrawRectangle(Pens.Blue, rDraw) 
    Else 
     brBack = Brushes.White 
     g.FillRectangle(brBack, e.Bounds) 
    End If 

    br = Nothing 
    brBack = Nothing 
    rDraw = Nothing 

    Try 
     s = DirectCast(sender, ComboBox).Items.Item(e.Index).ToString 
    Catch 
     s = "" 
    End Try 

    Dim x, y As Integer 

    x = e.Bounds.Left + 25 
    y = e.Bounds.Top + 1 
    Dim c As Color 
    Dim b As SolidBrush 
    c = Color.FromName(s) 
    b = New SolidBrush(c) 

    g.FillRectangle(b, x - 20, y + 2, 10, 10) 
    g.DrawString(s, DataGridView1.Font, Brushes.Black, x, y) 
End Sub 
+1

그러나 이것은 매우 효과적입니다. 참조 : http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/8b5f5bc9-db56-4843-9714-8d4e125fb592 및 http://msdn.microsoft.com/en-us/library/ 자세한 내용은 system.windows.forms.combobox.drawitem.aspx를 참조하십시오. 나는 이것을했고 그것이 효과가있다. 그러나 DataGrid에 여러 개의 드롭 다운 열이있는 경우 동일한 편집 컨트롤을 공유 할 수 있습니다. 처리되지 않으면 문제가 발생할 수 있습니다. – Tony

+0

답변에있는 코드는 열린 드롭 다운 목록과 선택한 후 포커스가있는 닫힌 목록에만 영향을 미칩니다. 초점을 잃으면 셀 내용이 기본 방식으로 다시 렌더링됩니다 (텍스트 만). – miroxlav

관련 문제