Creating custom Password column in datagridview

  • Thread starter Thread starter Skypea
  • Start date Start date
S

Skypea

Guest
Hello! I create a custom password column for datagridview using a textbox and UseSystemPasswordChar property of tis textbox. In editing mode, the cell value is displayed in password character (****), but when I select another cell/exit editing mode of cell, then the edited cell is showing the plain text (1234) not in password character (****). How to display the cell value as password character even not in editing mode?

this is my code:

Public Class PasswordColumn
Inherits DataGridViewColumn

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

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

MyBase.CellTemplate = value
End Set
End Property

End Class

Public Class PasswordCell
Inherits DataGridViewTextBoxCell

Public Sub New()

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 PasswordEditingControl =
CType(DataGridView.EditingControl, PasswordEditingControl)

ctl.UseSystemPasswordChar = True
End Sub

Public Overrides ReadOnly Property EditType() As Type
Get
Return GetType(PasswordEditingControl)
End Get
End Property

Public Overrides ReadOnly Property ValueType() As Type
Get
Return GetType(String)
End Get
End Property

End Class

Public Class PasswordEditingControl
Inherits TextBox
Implements IDataGridViewEditingControl

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

Public Sub New()
Me.UseSystemPasswordChar = True
End Sub

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

Get
Return Text
End Get

Set(ByVal value As Object)
Try
Text = CStr(value)
Catch
Text = ""
End Try
End Set

End Property

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

Return Text
End Function

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

Me.Font = dataGridViewCellStyle.Font
Me.ForeColor = dataGridViewCellStyle.ForeColor
Me.BackColor = dataGridViewCellStyle.BackColor
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

' No preparation needs to be done.
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(e As EventArgs)
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
Me.EditingControlDataGridView.CurrentCell.Value = Text
MyBase.OnTextChanged(e)
End Sub

End Class



Any solution?

Continue reading...
 
Back
Top