locked
Add a custom control to a DataGridView RRS feed

  • Question

  • Hello everyone.

    I've made a new control, it's a ComboBox with CheckBoxes that i named CheckedComboBox.
    I need this control on a DataGridView, so I create a new DataGridViewColumn but the problem is when I try to add items to the combobox,
    it gives me an error on the property EditingCheckedComboBox() on the return line.
    I saw a similar example in C# on the DataGridViewNumericUpDown Sample of the return line and I don't know if I convert it right or if something is missing:

    return this.DataGridView.EditingControl as DataGridViewNumericUpDownEditingControl;

     

    How can I solve this??? Here is the Code.

     

     

    Form1.vb

    Code Snippet

    Public Class Form1

     

        Private COptions As CheckedComboBoxColumn

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
            COptions.Items.Add("Option 1")
            COptions.Items.Add("Option 2")
            COptions.Items.Add("Option 3")

            Me.DataGridView1.Columns.Add(COptions)

        End Sub

    End Class

     

     

    CheckedComboBoxColumn.vb

    Code Snippet

    Public Class CheckedComboBoxColumn
        Inherits DataGridViewColumn

     

        Public Sub New()
            MyBase.New(New CheckedComboBoxCell())
        End Sub

     

        Public Overrides Property CellTemplate() As DataGridViewCell
            Get
                Return MyBase.CellTemplate
            End Get

     

            Set(ByVal value As DataGridViewCell)

                ' Ensure that the cell used for the template is a TimeCell.
                If (value IsNot Nothing) AndAlso Not value.GetType().IsAssignableFrom(GetType(CheckedComboBoxCell)) Then
                    Throw New InvalidCastException("Must be a CheckedComboBoxColumnCell")
                End If

                MyBase.CellTemplate = value

            End Set
        End Property

     

        Private ReadOnly Property CheckedComboBoxCellTemplate() As CheckedComboBoxCell
            Get
                Return Me.CellTemplate
            End Get 
        End Property

     

        Public ReadOnly Property Items() As CheckedComboBox.CheckedComboBox.ObjectCollection
            Get
                Return CheckedComboBoxCellTemplate.Items
            End Get 
        End Property
               
    End Class

     

     

    CheckedComboBoxCell

    Code Snippet

    Public Class CheckedComboBoxCell
        Inherits DataGridViewTextBoxCell

     

        Public Sub New()
            Me.Value = ""
        End Sub

     

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

            ' Set the value of the editing control to the current cell value.
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)

        End Sub

     

        Public Overrides ReadOnly Property EditType() As Type
            Get
                ' Return the type of the editing contol that CalendarCell uses.
                Return GetType(CheckedComboBoxEditingControl)
            End Get
        End Property

     

        Public Overrides ReadOnly Property ValueType() As Type
            Get
                ' Return the type of the value that CalendarCell contains.
                Return GetType(String)
            End Get
        End Property

     

        Public Overrides ReadOnly Property DefaultNewRowValue() As Object
            Get
                ' Use the current date and time as the default value.
                Return ""
            End Get
        End Property 
                
        Private ReadOnly Property EditingCheckedComboBox() As CheckedComboBoxEditingControl
            Get
                Return CType(DataGridView.EditingControl, CheckedComboBoxEditingControl)
            End Get 
        End Property

     

        Public ReadOnly Property Items() As CheckedComboBox.CheckedComboBox.ObjectCollection
            Get
                Try
                    Return Me.EditingCheckedComboBox.Items
                Catch ex As Exception
                    Return Nothing
                End Try

            End Get 
        End Property

     

    End Class

     

     

     

    CheckedComboBoxEditingControl.vb

    Code Snippet

    Class CheckedComboBoxEditingControl
        Inherits CheckedComboBox.CheckedComboBox
        Implements IDataGridViewEditingControl

     

        Private dataGridViewControl As DataGridView
        Private valueIsChanged As Boolean = False
        Private rowIndexNum As Integer

     

        Public Sub New()

        End Sub

     

        Public Property EditingControlFormattedValue() As Object _
            Implements IDataGridViewEditingControl.EditingControlFormattedValue

            Get
                Return Me.Text
            End Get

     

            Set(ByVal value As Object)
                If TypeOf value Is String Then
                    Me.Text = CStr(value)
                End If
            End Set

        End Property

     

        Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object _
            Implements IDataGridViewEditingControl.GetEditingControlFormattedValue

            Return Me.Text

        End Function

     

        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) _
            Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl

        End Sub

     

        Public Property EditingControlRowIndex() As Integer _
            Implements IDataGridViewEditingControl.EditingControlRowIndex

            Get
                Return rowIndexNum
            End Get

     

            Set(ByVal value As Integer)
                rowIndexNum = value
            End Set

        End Property

     

        Public Property EditingControlDataGridView() As DataGridView _
            Implements IDataGridViewEditingControl.EditingControlDataGridView

            Get
                Return dataGridViewControl
            End Get

     

            Set(ByVal value As DataGridView)
                dataGridViewControl = value
            End Set

        End Property

     

        Public Property EditingControlValueChanged() As Boolean _
            Implements IDataGridViewEditingControl.EditingControlValueChanged

            Get
                Return valueIsChanged
            End Get

     

            Set(ByVal value As Boolean)
                valueIsChanged = value
            End Set

        End Property

     

        Public ReadOnly Property EditingControlCursor() As Cursor _
            Implements IDataGridViewEditingControl.EditingPanelCursor

            Get
                Return MyBase.Cursor
            End Get

        End Property

     

    End Class

     

     

    Friday, September 14, 2007 11:43 PM

Answers

  • JackStephen,

     

    Based on your post, you would like to add the custom control called CheckedComboBox to the DataGridView and add the related columns. I created a new Windows Form application, added a DataGridView control, then create a DataGridViewComboBoxColumn on the control.

     

    I copied the code snippet as you provided, however, there is no code snippet on the CheckComboBox defined in the code. If it is convenient, please provide the code on CheckComboBox in order to help others to reproduce the problem better.

     

    Please also pay more attention to the code snippet such as Inherits CheckedComboBox.CheckedComboBox. Please make the name of properties and methods more clear.

     

    Then you can check the property of EditingCheckedComboBox, make sure the data format of return value is correct. I hope the following article and thread can provide you some idea on the issue:

     

    Build a Custom NumericUpDown Cell and Column for the DataGridView Control

     

    Displaying a usercontrol in a DataGridView cell

    Hope that can provide you some idea.

    Wednesday, September 19, 2007 3:06 AM

All replies

  • Please, can anybody help me??????

    Sunday, September 16, 2007 6:21 PM
  • JackStephen,

     

    Based on your post, you would like to add the custom control called CheckedComboBox to the DataGridView and add the related columns. I created a new Windows Form application, added a DataGridView control, then create a DataGridViewComboBoxColumn on the control.

     

    I copied the code snippet as you provided, however, there is no code snippet on the CheckComboBox defined in the code. If it is convenient, please provide the code on CheckComboBox in order to help others to reproduce the problem better.

     

    Please also pay more attention to the code snippet such as Inherits CheckedComboBox.CheckedComboBox. Please make the name of properties and methods more clear.

     

    Then you can check the property of EditingCheckedComboBox, make sure the data format of return value is correct. I hope the following article and thread can provide you some idea on the issue:

     

    Build a Custom NumericUpDown Cell and Column for the DataGridView Control

     

    Displaying a usercontrol in a DataGridView cell

    Hope that can provide you some idea.

    Wednesday, September 19, 2007 3:06 AM