2

I have used DataGridView to implement username-password UI. The passwords are shown in DataGridViewTextBoxColumn type column. How can I use the existing code for DataGridViewTextBoxColumn and implement password property for the text?

2 Answers 2

7

Handle the EditingControlShowing event and then cast the editing control to a TextBox and manually set the UseSystemPasswordChar to true:

TextBox passwordText = e.Control as TextBox;
if (passwordText != null)
{
    passwordText.UseSystemPasswordChar = true;
}

Edit

Could you try this :

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == “passwordDataGridViewTextBoxColumn” && e.Value != null)
    {
        dataGridView1.Rows[e.RowIndex].Tag = e.Value;
        e.Value = new String(‘*’, e.Value.ToString().Length);
    }
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (dataGridView1.CurrentRow.Tag != null)
        e.Control.Text = dataGridView1.CurrentRow.Tag.ToString();
}
2
  • This one works as long as I am editing the text inside text Box, I see that the password char vanishes as soon as I move to a different cell. Please help.
    – jetty
    Commented May 30, 2014 at 11:30
  • It works!!! I also set passwordText.PasswordChar = '*' inside dataGridView1_EditingControlShowing. Thanks for the help!
    – jetty
    Commented May 30, 2014 at 11:52
-1
if (e.ColumnIndex >= 0)
{
    if (dataGridView.Columns[e.ColumnIndex].Name == "Password" && e.Value != null)
    {
        dataGridView.Rows[e.RowIndex].Tag = e.Value;
        e.Value = new String('\u2022', e.Value.ToString().Length);
    }
}  
1
  • 1
    Perhaps show how to have your code called on the Cell_Formatting event to make the answer more complete? Commented Mar 14, 2015 at 10:12

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