2011-07-12 59 views
0

我在DataViewGrid中插入DataGridViewComboBoxColumn列。這工作正常。如何通過第二個小按鈕擴展DataGridView ComboBox列的編輯控件?

現在,我希望用戶不僅能夠選擇一個列表項目,而且能夠「鑽取」到組合框後面的列表中,從而允許他編輯(插入/刪除/更新)列表。

我認爲在ComboBox的下拉按鈕後面顯示一個「..」按鈕是一個好主意。推動它會導致一個對話框,在該對話框中可以維護列表。

什麼我在磕磕絆絆是:

  1. 我將如何創建這樣的自定義組合框?是否爲ComboBox(它是組合框的列創建爲其編輯控件的組合框的基礎)足夠開放以容納這樣的額外按鈕?那個小屁股的容器是什麼?ComboBox後代?
  2. 如何讓網格創建並處理這樣的自定義組合框?

我現在嘗試通過繼承DataGridViewComboBoxColum,在其CellTemplate分配使用DataGridViewComboBoxCell後代來解決這個問題,和壓倒一切的PositionWEditingPanelPositionEditingControl操縱面板和組合框的大小,所以我不得不空間微小按鈕。 這是正確的方法嗎?

或將我必須建立一個DataGridViewColumn後裔它創建了一個包含Panel一個DataGridView組合框編輯控件和微小的按鈕?我如何確保列保持組合框的關心,使其具有正確的項目等?

也許我聽起來困惑,但我可能是的VB代碼(DOH)周後....

回答

1

這是我想出了這個代碼。唯一缺少的是按鈕按下事件處理程序。

改進歡迎!

#Region "Custom column, cell and edit control for Combobox-with-a-'..'-Button" 


Public Class DataGridViewComboBoxExColumn 
    Inherits DataGridViewComboBoxColumn 
    Public Sub New() 
    MyBase.New() 
    CellTemplate = New DataGridViewComboBoxExCell() 
    End Sub 
    Public Overrides Property CellTemplate As DataGridViewCell 
    Get 
     Return MyBase.CellTemplate 
    End Get 
    Set(ByVal value As DataGridViewCell) 
     If (value IsNot Nothing) AndAlso Not value.GetType().IsAssignableFrom(GetType(DataGridViewComboBoxExCell)) Then 
     Throw New InvalidCastException("Must be a DataGridViewComboBoxExCell") 
     End If 
     MyBase.CellTemplate = value 
    End Set 
    End Property 
End Class 

Public Class DataGridViewComboBoxExCell 
    Inherits DataGridViewComboBoxCell 
    Dim HostingPanel As Panel 

    Public Sub New() 
    MyBase.New() 
    Dim TheButton As Button 
    HostingPanel = New Panel 
    HostingPanel.BorderStyle = BorderStyle.Fixed3D 
    HostingPanel.Padding = New Padding(0, 0, 0, 0) 

    HostingPanel.BackColor = Color.FromKnownColor(KnownColor.Control) 
    'HostingPanel.ForeColor = Color.Red ' Color.FromKnownColor(KnownColor.ButtonFace) 
    TheButton = New Button 
    TheButton.Text = "" 
    TheButton.BackColor = Color.FromKnownColor(KnownColor.ButtonFace) 
    TheButton.ImageList = DaCorFredProtMainForm.MainImageList 
    TheButton.ImageKey = "table_edit.png" 

    TheButton.Dock = DockStyle.Fill 
    HostingPanel.Controls.Add(TheButton) 
    End Sub 

    Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle) 
    MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle) 
    If Not Me.DataGridView.EditingPanel.Controls.Contains(HostingPanel) Then ' Should always be true 
     Me.DataGridView.EditingPanel.Controls.Add(HostingPanel) 
    End If 
    End Sub 

    Public Overrides Sub DetachEditingControl() 
    If Me.DataGridView.EditingPanel.Controls.Contains(HostingPanel) Then ' Should always be true 
     Me.DataGridView.EditingPanel.Controls.Remove(HostingPanel) 
    End If 
    MyBase.DetachEditingControl() 
    End Sub 

    Public Overrides ReadOnly Property EditType As Type 
    Get 
     Return MyBase.EditType 
    End Get 
    End Property 

    Public Overrides ReadOnly Property ValueType As Type 
    Get 
     Return MyBase.ValueType 
    End Get 
    End Property 

    Public Overrides Function PositionEditingPanel(ByVal cellBounds As System.Drawing.Rectangle, ByVal cellClip As System.Drawing.Rectangle, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal singleVerticalBorderAdded As Boolean, ByVal singleHorizontalBorderAdded As Boolean, ByVal isFirstDisplayedColumn As Boolean, ByVal isFirstDisplayedRow As Boolean) As System.Drawing.Rectangle 
    cellBounds.Width += cellBounds.Height 
    cellClip.Width += cellClip.Height 
    Return MyBase.PositionEditingPanel(cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow) 
    End Function 

    Public Overrides Sub PositionEditingControl(ByVal setLocation As Boolean, ByVal setSize As Boolean, ByVal cellBounds As System.Drawing.Rectangle, ByVal cellClip As System.Drawing.Rectangle, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal singleVerticalBorderAdded As Boolean, ByVal singleHorizontalBorderAdded As Boolean, ByVal isFirstDisplayedColumn As Boolean, ByVal isFirstDisplayedRow As Boolean) 
    MyBase.PositionEditingControl(setLocation, setSize, cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow) 
    Me.DataGridView.EditingControl.Width -= Me.DataGridView.EditingPanel.Height 
    HostingPanel.Width = Me.DataGridView.EditingPanel.Height 
    HostingPanel.Height = Me.DataGridView.EditingPanel.Height 
    HostingPanel.Location = New Point(DataGridView.EditingPanel.Size.Width - DataGridView.EditingPanel.Size.Height, 0) 
    End Sub 
End Class 

#End Region 
+0

列的寬度方面的一些調整後,我很好我自己的解決方案:) – TheBlastOne

+0

似乎有某種與微小的按鈕問題越來越插入面板的兩倍,但刪除了其中的一次一次有時。 – TheBlastOne

相關問題