4

I am creating an application in which user selects files and provides credentials to open that file. For that I have created three columns in a gridview.
User enters password in password column.
I want to display * in place of characters like we can create a textbox of password type.
I have tried this code on 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 above solution characters are displayed.
How can I solve this problem?

3 Answers 3

1

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

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 event e should have a ColumnIndex property :)

6
  • The handler for the EditingControlShowing will have by default DataGridViewEditingControlShowingEventArgs e as the second parameter, that's the one. Commented Apr 29, 2010 at 10:07
  • columnindex is not a member of DataGridViewEditingControlShowingEventArgs. Can you verify?
    – Preeti
    Commented Apr 30, 2010 at 6:44
  • sorry, mixed up the events a bit there, try something like dataGridView1.CurrentCell.ColumnIndex instead Commented Apr 30, 2010 at 7:06
  • now it works..But when i shift to other row.It again reset to original text :(
    – Preeti
    Commented Apr 30, 2010 at 7:18
  • For that you also have to handle the CellFormatting event, I'll update my answer with a sample. Commented Apr 30, 2010 at 7:35
0

One solution would be to create your own type of cell and assign that type to your password column. For example you'll add only the standard TextBoxPassword to it and then it should work as you wish.

On the MSDN there is more detailed description with source code available.

You'll just have to create your won kind of CellTemplate.

0

You can create a custom cell and column, and use a textbox with the password mask as your edit control.

To get around returning the clear text when you leave edit mode, you can store the actual password value in a seperate property of the cell, and in the GetFormattedValue event (i believe) you can return a string made up of entirely "*" characters to mask the regular 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.