Passwords in DataGridView

I need to display usernames and passwords in a DGV, and I have 2 questions :

  1. How do I display the password as a string of asterisks Right now, I am storing them in a List<string>, and then doing something like this

    foreach (DataGridViewRow row in dataGridView.Rows)
    {
    string s = "";

    for (int i = 0; i < passwordList[row.Index].Length; i++)
    s += "*";

    row.Cells["passwordColumn"].Value = s;
    }

    Is there a better way to do this

  2. When the user sorts by a column, and then "unhides" the passwords, the order of the passwords is incorrect. This is because upon unhiding, the passwords are read from the list in the old order (pre-sorted) and displayed. This problem does not occur when sorting occurs AFTER unhiding, since unhiding causes the asterisks to be replaced with the actual password string. Is there any way to work around this

Thanks a ton guys!



Answer this question

Passwords in DataGridView

  • Christoph Wienands

    In your code you are just changing the formatted value (even though you are setting the Value property it is the value property for the Formatting event). If you query the cell’s value you will notice that the cell’s value is the password in clear text.

    -mark

    DataGridView Program Manager

    Microsoft

    This post is provided "as-is"


  • Paul_Wilson

    I would use the CellFormatting event since this doesn't change the actual cell value, so sorting works correctly.

    -mark

    DataGridView Program Manager

    Microsoft

    This post is provided "as-is"


  • Jim Blackler

    thanks!!
  • yurixd

    If I understand correctly how to use the CellFormatting event, I would do something like this :

    private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {

    if
    (dataGridView.Columns[e.ColumnIndex].Name == "passwordColumn")
    {
    string s = "";

    for (int i = 0; i < passwordList[e.RowIndex].Length; i++)
    s +=
    "*";

    e.Value = s;
    }
    }

    I'm still having to change the Value of the cell. Am I missing something here


  • Passwords in DataGridView