How to create a password column in gridview?

I am creating an application in which the user selects files and provides credentials to open this file. For this, I created three columns in gridview.
The user enters the password in the password column.
I want to display * instead of characters, for example, we can create a text field such as a password.
I tried this code in the GridView_CellClick event:

 if (GridView.Columns[e.ColumnIndex].HeaderText == "Password") { txtPassword[e.RowIndex] = new TextBox(); txtPassword[e.RowIndex].Name = "txtPassword"+e.RowIndex; txtPassword[e.RowIndex].PasswordChar = '*'; txtPassword[e.RowIndex].Visible = true; txtPassword[e.RowIndex].TextChanged += new if (GridView.CurrentCell.Value == null) txtPassword[e.RowIndex].Text = ""; else txtPassword[e.RowIndex].Text = GridView.CurrentCell.Value.ToString(); txtPassword[e.RowIndex].Location = GridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex + 1, false).Location; txtPassword[e.RowIndex].Size = GridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex + 1, false).Size; txtPassword[e.RowIndex].Visible = true; txtPassword[e.RowIndex].Focus(); } 

But in the above characters the solutions are displayed.
How can I solve this problem?

+4
source share
3 answers

I think that it should work to handle the EditingControlShowing event, and add the following code in the handler:

 if(e.ColumnIndex == 2) { TextBox tb = e.Control as TextBox; if (tb != null) { tb.PasswordChar = '*'; } } 

CellFormatting event handler code:

 if(e.ColumnIndex = 2) { if(e.Value != null) { e.Value = New string("*", e.Value.ToString().Length); } } 

And in this case, e should have the ColumnIndex property :)

+1
source

One solution would be to create your own cell type and assign this type to your password column. For example, you add only the standard TextBoxPassword text to it, and then it will work as you wish.

MSDN has a more detailed description with available source code.

You just need to create your winning CellTemplate look .

0
source

You can create a custom cell and column and use a text field with a password mask as an edit control.

To get around returning clear text when you exit edit mode, you can save the actual password value in a separate property of the cell, and in the GetFormattedValue event (I suppose) you can return a string consisting entirely of "*" to mask the normal display.

 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 

for more information on what you are trying to do, visit this: http://www.vbforums.com/showthread.php?t=554744

0
source

Source: https://habr.com/ru/post/1308333/


All Articles