Problem in DataGridView

I have created my own custom datagridviewcell which supports richtextbox and maskedtextbox properties. But these properties will come only at the edit time. For example if the color of the text is set as red in richtextbox, it will be shown as red at the edit time. Once i go to other cell it will be shown in black colour. Also in masked text box if the mask is set as shortdate then only at the edit time it will show like "__/__/____". Remaining time it will not show text like this.

Can anybody tell me solution for this. (i.e., I want to show coloured text and "__/__/____" format in datagridviewcell at normal runtime, not only at edit time)




Answer this question

Problem in DataGridView

  • esb

    Maybe there is something wrong in your OnLeave method, would you please show us your code

  • FandangoAmeruso

    Hello Wang Chi,

    My DataGridViewCell Class and it's editing control Class codes are below. I have not used OnLeave Method. After this code I have attached my form code where I am using it.

    Public Class CustomDataGridViewRichTextBoxCell

    Inherits DataGridViewTextBoxCell

    Dim selectedColor As Color

    Dim selectedText As String

    Sub New()

    MyBase.New()

    End Sub

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

    Dim ctl As RichTextBoxEditingControl

    MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)

    ctl = CType(DataGridView.EditingControl, RichTextBoxEditingControl)

    ctl.Text = CType(Me.Value, String)

    End Sub

    Public Overrides ReadOnly Property EditType() As Type

    Get

    Return GetType(RichTextBoxEditingControl)

    End Get

    End Property

    Public Overrides ReadOnly Property ValueType() As Type

    Get

    Return GetType(String)

    End Get

    End Property

    Public Overrides ReadOnly Property DefaultNewRowValue() As Object

    Get

    Return Value

    End Get

    End Property

    Dim r As RichTextBox

    Dim k As Integer = 0

    End Class

    Class RichTextBoxEditingControl

    Inherits RichTextBox

    Implements IDataGridViewEditingControl

    Private dataGridViewControl As DataGridView

    Private valueIsChanged As Boolean = False

    Private rowIndexNum As Integer

    Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue

    Get

    Return Me.Text

    End Get

    Set(ByVal value As Object)

    Me.Text = value

    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

    Me.SelectAll()

    Me.SelectionColor = Color.Red

    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 False

    End Select

    End Function

    Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit

    'Not Required

    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

    Protected Overrides Sub OnTextChanged(ByVal eventargs As EventArgs)

    valueIsChanged = True

    Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)

    MyBase.OnTextChanged(eventargs)

    End Sub

    End Class

    My Form Code is below

    Public Class SampleDataGridView

    Private Sub SampleDataGridView_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    DataGridView1.RowCount = 13

    DataGridView1.Rows(0).Cells(3) = New CustomDataGridViewRichTextBoxCell()

    Dim ref As CustomDataGridViewRichTextBoxCell = DataGridView1.Rows(0).Cells(3)

    ref.Value = "21:ABC:I:XYZ"

    End Sub

    End Class

    What I want here is 21, ABC, I and XYZ all of them should be seen in different colour. At the time of editing if i do it, then once i go to other cell it shows normal text. Similarly I want mask to be shown after completion of the edit in a overridden maskedtextboxcell.

    How do i do this.



  • Problem in DataGridView