Announcement

Collapse
No announcement yet.

Usercontrol in Datagridviewcolumn

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Usercontrol in Datagridviewcolumn

    Hallo,

    ich bin gerade daran ein Usercontrol in eine datagridviewcolumn zu packen.
    Im großen und ganzen funktioniert das auch schon.
    Das einzige Problem was ich noch habe ist das der Value vom Editing Control nicht an die Datagridviewcell übergeben wird.
    D.h. auch beim aufrufen des Editingcontrols wird der Wert vom Editingcontrol immer wieder auf den Defaultwert der Datagridviewcell zurückgesetzt.

    Vielleicht könnt ihr mir ja weiterhelfen, hier mal der Code:

    Code:
    Imports System
    Imports System.Windows.Forms
    
    Public Class MyCheckboxColumn
        Inherits DataGridViewColumn
    
        Public Sub New()
            MyBase.New(New MyCheckboxCell())
        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(MyCheckboxCell)) _
                    Then
                    Throw New InvalidCastException("Must be a MyCheckboxCell")
                End If
                MyBase.CellTemplate = value
    
            End Set
        End Property
    
    End Class
    
    Public Class MyCheckboxCell
        Inherits DataGridViewCell
    
        Public Sub New()
            ValueType = Type.GetType("System.Boolean")
        End Sub
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
            ByVal initialFormattedValue As Object, _
            ByVal dataGridViewCellStyle As DataGridViewCellStyle)
    
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
                dataGridViewCellStyle)
    
            Dim ctl As MyCheckboxEditingControl = _
                CType(DataGridView.EditingControl, MyCheckboxEditingControl)
    
            If (Me.Value Is Nothing) Then
                ctl.Checked = CType(Me.DefaultNewRowValue, Boolean)
            Else
                ctl.Checked = CType(Me.Value, Boolean)
            End If
        End Sub
    
    
        Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal cellState As System.Windows.Forms.DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
            MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
    
            graphics.FillRectangle(Brushes.White, New Rectangle(cellBounds.X, cellBounds.Y, cellBounds.Width, cellBounds.Height))
            If value = True Then
                graphics.DrawImage(My.Resources.CheckedCheckbox, New Point(cellBounds.X, cellBounds.Y))
            Else
                graphics.DrawImage(My.Resources.Unchecked_Checkbox, New Point(cellBounds.X, cellBounds.Y))
            End If
    
        End Sub
        Public Overrides ReadOnly Property EditType() As Type
            Get
                Return GetType(MyCheckboxEditingControl)
            End Get
        End Property
    
    
        Public Overrides ReadOnly Property DefaultNewRowValue() As Object
            Get
                Return True
            End Get
        End Property
    
    
        Public Overrides ReadOnly Property FormattedValueType() As System.Type
            Get
                Return Type.GetType("System.Boolean")
            End Get
        End Property
    
        Protected Overrides Function GetFormattedValue(ByVal value As Object, ByVal rowIndex As Integer, ByRef cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal valueTypeConverter As System.ComponentModel.TypeConverter, ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter, ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object
            If MyBase.GetFormattedValue(value, rowIndex, cellStyle, valueTypeConverter, formattedValueTypeConverter, context) Is Nothing Then
                Return False
            Else
                Return MyBase.GetFormattedValue(value, rowIndex, cellStyle, valueTypeConverter, formattedValueTypeConverter, context)
            End If
        End Function
    
        Protected Overrides Sub OnClick(ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
            MyBase.OnClick(e)
            DataGridView.BeginEdit(False)
        End Sub
    
    End Class
    
    Class MyCheckboxEditingControl
        Inherits MyCheckbox
        Implements IDataGridViewEditingControl
    
        Private dataGridViewControl As DataGridView
        Private valueIsChanged As Boolean = False
        Private rowIndexNum As Integer
    
        Public Sub New()
            AddHandler Me.CheckedChanged, AddressOf CheckedChangedEvent
    
        End Sub
    
        Private Sub CheckedChangedEvent()
            ' Notify the DataGridView that the contents of the cell have changed.
            valueIsChanged = True
            Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
    
        End Sub
        Public Property EditingControlFormattedValue() As Object _
            Implements IDataGridViewEditingControl.EditingControlFormattedValue
    
            Get
                Return Me.Checked
            End Get
    
            Set(ByVal value As Object)
                Try
                    Me.Checked = Convert.ToBoolean(value)
                Catch
                    Me.Checked = True
                End Try
            End Set
    
        End Property
    
        Public Function GetEditingControlFormattedValue(ByVal context _
            As DataGridViewDataErrorContexts) As Object _
            Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
    
            Return Me.Checked
    
        End Function
    
        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
            DataGridViewCellStyle) _
            Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
    
            Me.Font = dataGridViewCellStyle.Font
    
    
        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 Function EditingControlWantsInputKey(ByVal key As Keys, _
            ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
            Implements IDataGridViewEditingControl.EditingControlWantsInputKey
    
            Select Case key And Keys.KeyCode
                Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
                    Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp
    
                    Return True
    
                Case Else
                    Return Not dataGridViewWantsInputKey
            End Select
    
        End Function
    
        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
            Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
    
        End Sub
    
        Public ReadOnly Property RepositionEditingControlOnValueChange() _
            As Boolean Implements _
            IDataGridViewEditingControl.RepositionEditingControlOnValueChange
    
            Get
                Return False
            End Get
    
        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



    mfg
Working...
X