in

Platinum Bay

Peace, Love and Visual Studio Team System

.NETicated

Editable ComboBox

A colleague ran into an issue awhile back trying to edit data in a DataGrid. He had a ComboBox, and wanted to be able to edit the text to a value not bound to it.

The answer is really simple, but not well documented.

The first step is to create a class that derives from DataGridViewComboBoxCell. This is where the magic happens. By default, the DropDownStyle of the ComboBox in this column is set to ComboBoxStyle.DropDownList, which is not editable. We can change that by setting the DropDownStyle to ComboBoxStyle.DropDown.

Public Class EditableDataGridViewComboBoxCell
    Inherits DataGridViewComboBoxCell

    Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)

        Dim box1 As ComboBox = CType(MyBase.DataGridView.EditingControl, ComboBox)

        If (Not box1 Is Nothing) Then
            box1.DropDownStyle = ComboBoxStyle.DropDown
        End If
    End Sub
End Class

The next step is to create a control that derives from DataGridViewComboBoxColumn and sets the CellTemplate property to an instance of our EditableDataGridViewComboBoxCell

Public Class EditableDataGridViewComboBoxColumn
    Inherits DataGridViewComboBoxColumn

    Public Sub New()
        Me.CellTemplate = New EditableDataGridViewComboBoxCell
    End Sub
End Class

Finally in your form, set your column to be of type EditableDataGridViewComboBoxColumn.

Once you have this implementation in place, you can do things like override the change event, and save your new value to the underlying datasource.

Have fun!

Published Jun 06 2007, 02:26 AM by Steve
Filed under: ,

Comments

No Comments

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews has been working as a developer for more than 8 years. During this time, he has designed and developed applications in such widely varying areas as trust accounting, medical information management, supply chain management, and retail systems. He has firsthand developer experience with a variety of languages, including Java, VB, and .NET. Most recently, he has been immersed in SharePoint. He is currently employed at RDA Corporation in Philadelphia, PA, as a Software Engineer and a team member in the Architectural Guidance evangelism team. Steve is also an MTCS (x2), ICSOO, and .NET fanatic.
Powered by Community Server (Commercial Edition), by Telligent Systems
© Platinum Bay | Some Rights Reserved Creative Commons License

Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or annonymous comments) - so keep it polite, please.