PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] [2008] Datagridview Password Column Cellformatting-VBForums
Results 1 to 22 of 22

Thread: [RESOLVED] [2008] Datagridview Password Column Cellformatting

  1. #1

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Resolved [RESOLVED] [2008] Datagridview Password Column Cellformatting

    Hi Guys,

    I have a password column in my datagridview, and I'd like to display the characters as a * while editing and even displaying. I have the following code but when saving to the database the password gets saved as ***** for example.:

    Code:
    Private Sub dgvUsers_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvUsers.CellFormatting
            If dgvUsers.Columns(e.ColumnIndex).Name.Equals("Password") Then
    
                If (Not e.Value Is Nothing) Then
                    e.Value = New String(CChar("*"), e.Value.ToString.Length)
    
                End If
    
            End If
        End Sub
    Code:
    Private Sub dgvUsers_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvUsers.EditingControlShowing
    
            If (CType(sender, DataGridView).CurrentCell.ColumnIndex <> 4) Then
    
                If (CType(sender, DataGridView).CurrentCell.ColumnIndex = 3) Then
    
                    CType(e.Control, TextBox).PasswordChar = CChar("*")
    
                Else
    
                    CType(e.Control, TextBox).PasswordChar = Char.MinValue
    
                End If
    
            End If
        End Sub
    Please advise on the best way to accomplish this

  2. #2

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] Datagridview Password Column Cellformatting

    please help me someone

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,610

    Re: [2008] Datagridview Password Column Cellformatting

    It looks like setting e.Value actually sets the Value of the cell itself, which is obviously not what you want. I'd suggest creating your own DataGridViewPasswordTextBoxColumn and DataGridViewPasswordTextBoxCell classes. They would inherit the existing text box classes and there would be very little to change. The main thing would be that you could override the GetFormattedValue method and return a string of mask characters without changing the actual value of the cell. You could also provide a PasswordChar property on the column that would propagate down to the cells and editing control.

  4. #4

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] Datagridview Password Column Cellformatting

    thanks JM,

    will give it a go.

  5. #5

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] Datagridview Password Column Cellformatting

    Hi JM, this is my class so far. I don't understand this, please help me:

    The main thing would be that you could override the GetFormattedValue method and return a string of mask characters without changing the actual value of the cell. You could also provide a PasswordChar property on the column that would propagate down to the cells and editing control
    Code:
    Imports System
    Imports System.Windows.Forms
    Public Class DataGridViewPasswordTextBoxColumn
        Inherits DataGridViewColumn
    
        Public Sub New()
            MyBase.New(New PasswordTextboxCell)
        End Sub
        Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
            Get
                Return MyBase.CellTemplate
            End Get
            Set(ByVal value As System.Windows.Forms.DataGridViewCell)
    
                If value IsNot Nothing AndAlso _
                    Not value.GetType.IsAssignableFrom(GetType(PasswordTextboxCell)) _
                    Then
                    Throw New InvalidCastException("Must be TextboxCell")
                End If
    
                MyBase.CellTemplate = value
            End Set
        End Property
    
    End Class
    Public Class PasswordTextboxCell
        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)
    
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
                                            dataGridViewCellStyle)
    
            Dim ctl As PasswordTextBoxEditingControl = _
                CType(DataGridView.EditingControl, PasswordTextBoxEditingControl)
    
            If IsDBNull(Me.Value) Then
                ctl.Text = ""
            Else
                ctl.Text = CType(Me.Value, String)
            End If
    
        End Sub
        Public Overrides ReadOnly Property EditType() As Type
            Get
                ' Return the type of the editing contol that CalendarCell uses.
                Return GetType(PasswordTextBoxEditingControl)
            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 String.Empty
            End Get
        End Property
    End Class
    Public Class PasswordTextBoxEditingControl
        Inherits TextBox
        Implements IDataGridViewEditingControl
    
        Private dataGridViewControl As DataGridView
        Private valueIsChanged As Boolean = False
        Private rowIndexNum As Integer
        Public Sub New()
    
        End Sub
        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle) _
            Implements System.Windows.Forms.IDataGridViewEditingControl.ApplyCellStyleToEditingControl
            Me.Font = dataGridViewCellStyle.Font
        End Sub
    
        Public Property EditingControlDataGridView() As System.Windows.Forms.DataGridView Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView
            Get
                Return dataGridViewControl
            End Get
            Set(ByVal value As System.Windows.Forms.DataGridView)
                dataGridViewControl = value
            End Set
        End Property
    
        Public Property EditingControlFormattedValue() As Object _
            Implements System.Windows.Forms.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 Property EditingControlRowIndex() As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
            Get
                Return rowIndexNum
            End Get
            Set(ByVal value As Integer)
                rowIndexNum = value
            End Set
        End Property
    
        Public Property EditingControlValueChanged() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
            Get
                Return valueIsChanged
            End Get
            Set(ByVal value As Boolean)
                valueIsChanged = value
            End Set
        End Property
    
        Public Function EditingControlWantsInputKey(ByVal keyData As System.Windows.Forms.Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlWantsInputKey
    
        End Function
    
        Public ReadOnly Property EditingPanelCursor() As System.Windows.Forms.Cursor Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
            Get
    
            End Get
        End Property
    
        Public Function GetEditingControlFormattedValue(ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
            Return Me.Text
        End Function
    
        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
    
        End Sub
    
        Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
            Get
                Return False
            End Get
        End Property
        Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
            valueIsChanged = True
            Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
            MyBase.OnTextChanged(e)
        End Sub
    
    
    End Class

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,610

    Re: [2008] Datagridview Password Column Cellformatting

    The GetFormattedValue method of a DataGridViewCell takes the actual value that the cell contains and processes it, returning a formatted value for display in the UI. This will allow you to return a string containing password mask characters for display without changing the actual value of the cell.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,610

    Re: [2008] Datagridview Password Column Cellformatting

    I could be wrong but I'd also expect that it would be easier to inherit DataGridViewTextBoxEditingControl than to inherit TextBox and provide your own implementation of IDataGridViewEditingControl.

  8. #8

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] Datagridview Password Column Cellformatting

    Hi JM,

    I haven't got much further with this . I'm not very good at classes. Please help me with the next step.

  9. #9

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] Datagridview Password Column Cellformatting

    I've done it . Thank you JM. I ain't sure if my class is 100% correct but it works. Here is the class incase anyone else ever needs it. I've also provided a masked property for the editing control. JM if you could please check this class and point out any faults i would greatly appreciate it:

    Code:
    Imports System
    Imports System.Windows.Forms
    Public Class DataGridViewPasswordTextBoxColumn
        Inherits DataGridViewColumn
    
        Public Sub New()
            MyBase.New(New PasswordTextboxCell)
        End Sub
        Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
            Get
                Return MyBase.CellTemplate
            End Get
            Set(ByVal value As System.Windows.Forms.DataGridViewCell)
    
                If value IsNot Nothing AndAlso _
                    Not value.GetType.IsAssignableFrom(GetType(PasswordTextboxCell)) _
                    Then
                    Throw New InvalidCastException("Must be TextboxCell")
                End If
    
                MyBase.CellTemplate = value
            End Set
        End Property
    
    End Class
    Public Class PasswordTextboxCell
        Inherits DataGridViewTextBoxCell
    
        Public Sub New()
            Me.Value = ""
        End Sub
        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
            Dim strVal As String
    
            strVal = New String(CChar("*"), value.ToString.Length)
    
            Return MyBase.GetFormattedValue(strVal, rowIndex, cellStyle, valueTypeConverter, formattedValueTypeConverter, context)
    
        End Function
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
        ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
    
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
                                            dataGridViewCellStyle)
    
            Dim ctl As PasswordTextBoxEditingControl = _
                CType(DataGridView.EditingControl, PasswordTextBoxEditingControl)
    
            If IsDBNull(Me.Value) Then
                ctl.Text = ""
    
    
            Else
                ctl.Text = CType(Me.Value, String)
                ctl.PasswordChar = "*"
                ctl.Mask = "*"
            End If
    
        End Sub
        Public Overrides ReadOnly Property EditType() As Type
            Get
                ' Return the type of the editing contol that CalendarCell uses.
                Return GetType(PasswordTextBoxEditingControl)
            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
    End Class
    Public Class PasswordTextBoxEditingControl
        Inherits TextBox
        Implements IDataGridViewEditingControl
    
        Private dataGridViewControl As DataGridView
        Private valueIsChanged As Boolean = False
        Private rowIndexNum As Integer
        Public Sub New()
    
        End Sub
        Private m_strMask As String
        Public Property Mask() As String
            Get
                Return m_strMask
            End Get
            Set(ByVal value As String)
                m_strMask = value
            End Set
        End Property
        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle) _
            Implements System.Windows.Forms.IDataGridViewEditingControl.ApplyCellStyleToEditingControl
            Me.Font = dataGridViewCellStyle.Font
        End Sub
    
        Public Property EditingControlDataGridView() As System.Windows.Forms.DataGridView Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView
            Get
                Return dataGridViewControl
            End Get
            Set(ByVal value As System.Windows.Forms.DataGridView)
                dataGridViewControl = value
            End Set
        End Property
    
        Public Property EditingControlFormattedValue() As Object _
            Implements System.Windows.Forms.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 Property EditingControlRowIndex() As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
            Get
                Return rowIndexNum
            End Get
            Set(ByVal value As Integer)
                rowIndexNum = value
            End Set
        End Property
    
        Public Property EditingControlValueChanged() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
            Get
                Return valueIsChanged
            End Get
            Set(ByVal value As Boolean)
                valueIsChanged = value
            End Set
        End Property
    
        Public Function EditingControlWantsInputKey(ByVal keyData As System.Windows.Forms.Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlWantsInputKey
    
        End Function
    
        Public ReadOnly Property EditingPanelCursor() As System.Windows.Forms.Cursor Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
            Get
    
            End Get
        End Property
    
        Public Function GetEditingControlFormattedValue(ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
            Return Me.Text
        End Function
    
        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
    
        End Sub
    
        Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
            Get
                Return False
            End Get
        End Property
        Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
            valueIsChanged = True
            Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
            MyBase.OnTextChanged(e)
        End Sub
    
    
    End Class
    vb.net usage:

    Code:
    Dim passwordtextboxcolumn As New DataGridViewPasswordTextBoxColumn
    With passwordtextboxcolumn
                    .HeaderText = "Password"
                    .DataPropertyName = "Password"
                               End With

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,610

    Re: [2008] Datagridview Password Column Cellformatting

    Well done. Maybe you're better with classes than you thought and you posted asking for help a bit prematurely.

    That said, you've probably done more than is required. For instance, there's no need for your custom editing control because the DataGridViewTextBoxEditingControl class already supports the functionality you need. Here's my version that I was preparing in the mean time:
    vb.net Code:
    1. Imports System.ComponentModel
    2.  
    3. Public Class DataGridViewPasswordTextBoxColumn
    4.     Inherits DataGridViewColumn
    5.  
    6.     Private _passwordChar As Char
    7.     Private _useSystemPasswordChar As Boolean
    8.  
    9.     <Category("Password")>
    10.     Public Property PasswordChar As Char
    11.         Get
    12.             Return _passwordChar
    13.         End Get
    14.         Set
    15.             If _passwordChar <> Value Then
    16.                 _passwordChar = Value
    17.  
    18.                 Dim cell = TryCast(CellTemplate, DataGridViewPasswordTextBoxCell)
    19.  
    20.                 If cell IsNot Nothing Then
    21.                     'Update the template cell.
    22.                     cell.PasswordChar = Value
    23.                 End If
    24.  
    25.                 If DataGridView IsNot Nothing Then
    26.                     'Update each existing cell in the column.
    27.                     For Each row As DataGridViewRow In DataGridView.Rows
    28.                         cell = TryCast(row.Cells(Index), DataGridViewPasswordTextBoxCell)
    29.  
    30.                         If cell IsNot Nothing Then
    31.                             cell.PasswordChar = Value
    32.                         End If
    33.                     Next
    34.  
    35.                     'Force a repaint so the grid reflects the current property value.
    36.                     DataGridView.Refresh()
    37.                 End If
    38.             End If
    39.         End Set
    40.     End Property
    41.  
    42.     <Category("Password")>
    43.     Public Property UseSystemPasswordChar As Boolean
    44.         Get
    45.             Return _useSystemPasswordChar
    46.         End Get
    47.         Set
    48.             If _useSystemPasswordChar <> Value Then
    49.                 _useSystemPasswordChar = Value
    50.  
    51.                 Dim cell = TryCast(CellTemplate, DataGridViewPasswordTextBoxCell)
    52.  
    53.                 If cell IsNot Nothing Then
    54.                     'Update the template cell.
    55.                     cell.UseSystemPasswordChar = Value
    56.                 End If
    57.  
    58.                 If DataGridView IsNot Nothing Then
    59.                     'Update each existing cell in the column.
    60.                     For Each row As DataGridViewRow In DataGridView.Rows
    61.                         cell = TryCast(row.Cells(Index), DataGridViewPasswordTextBoxCell)
    62.  
    63.                         If cell IsNot Nothing Then
    64.                             cell.UseSystemPasswordChar = Value
    65.                         End If
    66.                     Next
    67.  
    68.                     'Force a repaint so the grid reflects the current property value.
    69.                     DataGridView.Refresh()
    70.                 End If
    71.             End If
    72.         End Set
    73.     End Property
    74.  
    75.     Public Sub New()
    76.         MyBase.New(New DataGridViewPasswordTextBoxCell)
    77.     End Sub
    78.  
    79.     Public Overrides Function Clone() As Object
    80.         Dim copy = DirectCast(MyBase.Clone(), DataGridViewPasswordTextBoxColumn)
    81.  
    82.         copy.PasswordChar = _passwordChar
    83.         copy.UseSystemPasswordChar = _useSystemPasswordChar
    84.  
    85.         Return copy
    86.     End Function
    87.  
    88. End Class
    vb.net Code:
    1. Imports System.ComponentModel
    2.  
    3. Public Class DataGridViewPasswordTextBoxCell
    4.     Inherits DataGridViewTextBoxCell
    5.  
    6.     Private editingControlPasswordChar As Char
    7.     Private editingControlUseSystemPasswordChar As Boolean
    8.  
    9.     Public Property PasswordChar As Char
    10.     Public Property UseSystemPasswordChar As Boolean
    11.  
    12.     Public Overrides Function Clone() As Object
    13.         Dim copy = DirectCast(MyBase.Clone(), DataGridViewPasswordTextBoxCell)
    14.  
    15.         copy.PasswordChar = PasswordChar
    16.         copy.UseSystemPasswordChar = UseSystemPasswordChar
    17.  
    18.         Return copy
    19.     End Function
    20.  
    21.     Protected Overrides Function GetFormattedValue(value As Object,
    22.                                                    rowIndex As Integer,
    23.                                                    ByRef cellStyle As DataGridViewCellStyle,
    24.                                                    valueTypeConverter As TypeConverter,
    25.                                                    formattedValueTypeConverter As TypeConverter,
    26.                                                    context As DataGridViewDataErrorContexts) As Object
    27.         Dim formattedValue As Object
    28.  
    29.         If UseSystemPasswordChar AndAlso value IsNot Nothing Then
    30.             'Display the system password character in place of each actual character.
    31.             'TODO: Determine the actual system password character instead of hard-coding this value.
    32.             formattedValue = New String(Convert.ToChar(&H25CF), value.ToString().Length)
    33.         ElseIf PasswordChar <> Char.MinValue AndAlso value IsNot Nothing Then
    34.             'Display the user-defined password character in place of each actual character.
    35.             formattedValue = New String(PasswordChar, value.ToString().Length)
    36.         Else
    37.             'Display the value as is.
    38.             formattedValue = MyBase.GetFormattedValue(value,
    39.                                                       rowIndex,
    40.                                                       cellStyle,
    41.                                                       valueTypeConverter,
    42.                                                       formattedValueTypeConverter,
    43.                                                       context)
    44.         End If
    45.  
    46.         Return formattedValue
    47.     End Function
    48.  
    49.     Public Overrides Sub InitializeEditingControl(rowIndex As Integer,
    50.                                                   initialFormattedValue As Object,
    51.                                                   dataGridViewCellStyle As DataGridViewCellStyle)
    52.         'Use the cell's Value rather than the initialFormattedValue because the latter contains the mask characters.
    53.         'We want the editing control to do the masking itself rather than actually contain the mask characters.
    54.         MyBase.InitializeEditingControl(rowIndex, Value, dataGridViewCellStyle)
    55.  
    56.         With DirectCast(DataGridView.EditingControl, TextBox)
    57.             'Remember the current password properties of the editing control.
    58.             editingControlPasswordChar = .PasswordChar
    59.             editingControlUseSystemPasswordChar = .UseSystemPasswordChar
    60.  
    61.             'Set the new password properties of the editing control.
    62.             .PasswordChar = PasswordChar
    63.             .UseSystemPasswordChar = UseSystemPasswordChar
    64.         End With
    65.     End Sub
    66.  
    67.     Public Overrides Sub DetachEditingControl()
    68.         MyBase.DetachEditingControl()
    69.  
    70.         With DirectCast(DataGridView.EditingControl, TextBox)
    71.             .Clear()
    72.  
    73.             'Reset the old password properties of the editing control.
    74.             .PasswordChar = editingControlPasswordChar
    75.             .UseSystemPasswordChar = editingControlUseSystemPasswordChar
    76.         End With
    77.     End Sub
    78.  
    79. End Class
    Note that they can also be added to your grid in the designer.

    EDIT: I have fixed a bug in the DataGridViewPasswordTextBoxCell where editing a second time would cause the mask characters to be stored instead of the original characters they were masking. The solution was to change the first line of the InitializeEditingControl method from this:
    vb.net Code:
    1. MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
    to this:
    vb.net Code:
    1. 'Use the cell's Value rather than the initialFormattedValue because the latter contains the mask characters.
    2. 'We want the editing control to do the masking itself rather than actually contain the mask characters.
    3. MyBase.InitializeEditingControl(rowIndex, Value, dataGridViewCellStyle)
    The updated code also includes the fix mentioned in post #18.
    Attached Files Attached Files
    Last edited by jmcilhinney; May 23rd, 2018 at 02:03 AM.

  11. #11

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] Datagridview Password Column Cellformatting

    Wow, thanks JM. I really appreciate your help and effort. I couldn't have done it without you.. Will look at your great example now.

  12. #12
    New Member
    Join Date
    Apr 2009
    Posts
    1

    Re: [RESOLVED] [2008] Datagridview Password Column Cellformatting

    Thanks Nitesh, and JM. especially!

    The provided solution is the best that I have ever seen!

    PS: Maybe one slight modification: Because Textbox think '*' instead of the big black dot as the "SystemPasswordChar", the last sentence in "InitializeEditingControl" may be changed to ".UseSystemPasswordChar = false" to be controlled totally by the customerized class.

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,610

    Re: [RESOLVED] [2008] Datagridview Password Column Cellformatting

    Quote Originally Posted by jiucenglou View Post
    Maybe one slight modification: Because Textbox think '*' instead of the big black dot as the "SystemPasswordChar", the last sentence in "InitializeEditingControl" may be changed to ".UseSystemPasswordChar = false" to be controlled totally by the customerized class.
    No, that's not a good idea. The way I've done it the mask character is already controlled by the values the user sets in the column properties. That method is simply applying those property values to the edting control, which is exactly what should happen.

  14. #14
    New Member
    Join Date
    Oct 2009
    Posts
    1

    Re: [2008] Datagridview Password Column Cellformatting

    I will use the code you (Nitesh & JMcilhinney) had proposed to implemente the "Password char on DataGrid Column" problem. I like the way you code (the way I want become to code ). I have not tested it, but in case that It works the way I want, I want to quote in my code credits to you (both). Please (If u want) send me e-mail or VbForums Contact.

  15. #15
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Re: [RESOLVED] [2008] Datagridview Password Column Cellformatting

    Hi JM,

    Could you please show me one example how to use the class in my code?

    Thanks and regards,

    Treya

  16. #16
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,610

    Re: [RESOLVED] [2008] Datagridview Password Column Cellformatting

    Quote Originally Posted by treyacon View Post
    Hi JM,

    Could you please show me one example how to use the class in my code?

    Thanks and regards,

    Treya
    They get used exactly the same way as any other DataGridViewColumn. Once you've added the classes to your project and built it, you can then add a column to your grid and select DataGridViewPasswordTextBoxColumn as the type. You can then set its properties just like any other column type.

  17. #17
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Re: [RESOLVED] [2008] Datagridview Password Column Cellformatting

    Hi JM,

    Thanks for the great solution!.

    But i got "Conversion from type 'DBNull' to type 'String' is not valid." error.
    i can only add on error resume next handle on top of
    Protected Overrides Function GetFormattedValue

    Any advise for this error would be much appreciated.

    Regards,
    Treyacon

  18. #18
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,610

    Re: [RESOLVED] [2008] Datagridview Password Column Cellformatting

    Quote Originally Posted by treyacon View Post
    Hi JM,

    Thanks for the great solution!.

    But i got "Conversion from type 'DBNull' to type 'String' is not valid." error.
    i can only add on error resume next handle on top of
    Protected Overrides Function GetFormattedValue

    Any advise for this error would be much appreciated.

    Regards,
    Treyacon
    Hmmm... maybe I didn't ever test it with null values. Try changing CStr(value) to value.ToString()

  19. #19
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Re: [RESOLVED] [2008] Datagridview Password Column Cellformatting

    Hi JM,

    value.tostring is working.

    Thanks!

    Treyacon

  20. #20
    Addicted Member
    Join Date
    Oct 2012
    Posts
    166

    Re: [RESOLVED] [2008] Datagridview Password Column Cellformatting

    Hello there.
    I know this thread is really old and that I'm necroing this old treasure, but I'm not understanding how to use the classes mentioned here, even though I have tried, but nothing changed.
    This is what I have:

    Code:
    Try
                Dim passwordtextboxcolumn As New DataGridViewPasswordTextBoxColumn
                With passwordtextboxcolumn
                    .HeaderText = "Password"
                    .DataPropertyName = "Password"
                End With
                taLogins.Fill(tLogins)
    
                dgvLoginsList.DataSource = tLogins
                dgvLoginsList.Columns.Add(passwordtextboxcolumn)
                dgvLoginsList.Columns(2).Visible = False
                dgvLoginsList.Columns(7).DisplayIndex = 2
    
                Label2.Text = "Utilizadores: " & tLogins.Rows.Count
            Catch ex As Exception
    
            End Try
    So, I basically have a tableadapter "taLogins", that will load data into the table "tlogins". Then, I display the data on datagridview, from that table.
    This will, of course, generate named columns and on this case a column named Password will be add on the datagridview.
    To my sense (and I don't know if I'm going the right path here) after adding the column "passwordtextboxcolumn", I hide the already name column "Password" and set the índex of the added column as the same as the one I've just hidden.
    By doing this process I get the column, with overridable properties written on the classes on the desired índex. Although, I still have the data on this column being showed as plain text, instead of the "*" mask as it was supposed...

    I hope I was able to explain my problem and hope that someone would please explain to me what I need to do.

    Edit* Damn, I feel stupid now. I just noticed I needed to add the following line:

    passwordtextboxcolumn.PasswordChar = "*"

    It's working wonders now.
    Last edited by Simbiose; Mar 31st, 2014 at 07:03 AM.

  21. #21
    Member
    Join Date
    Apr 2018
    Posts
    32

    Re: [RESOLVED] [2008] Datagridview Password Column Cellformatting

    Ok, I see what you're saying. The code works the same without 'new'.
    I thought it was working perfectly, but with further testing...
    If doubleclicking a cell to copy it (see code below), it then makes every cell UseSystemPasswordChar while typing. It reverts to normal text when clicking elsewhere. I don't get it.

    Code:
        Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles MyGrid.CellDoubleClick
    
            Dim cell As DataGridViewCell = MyGrid.CurrentCell
            If Not IsDBNull(cell.Value) And Not cell.Value.ToString = "" And Not cell.Value Is Nothing Then
                Select Case e.ColumnIndex
                    Case 5
                        Exit Sub
                    Case 2
                        If Not cell.Value.startswith("http://") And Not cell.Value.startswith("https://") Then
                            cell.Value = "http://" & cell.Value
                        End If
                        Process.Start(cell.Value)
                    Case Else
                        If cell.Value.ToString IsNot Nothing Then
                            My.Computer.Clipboard.SetText(cell.Value.ToString)
                        End If
                End Select
            End If
    
        End Sub
    I can't figure out if the problem is this sub, mousedown, or mouseup.
    Last edited by Amerigoware; Apr 16th, 2018 at 10:01 AM.

  22. #22
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,610

    Re: [RESOLVED] [2008] Datagridview Password Column Cellformatting

    @Amerigoware, because of the age of this thread and the fact that it was marked Resolved, I suggested to the mods that your posts be moved to their own thread, which was done. You've now posted here again with zero context. The thread created for you is here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width