Rob Kraft's Software Development Blog

Software Development Insights

How to create a Password column using the DataGridView

Posted by robkraft on December 19, 2008

If you want to hide the password value in a Visual Studio DataGridView control, you can use the code below.  If a user clicks into the cell to edit the value, then the current value becomes visible.
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();
}

I found the example above on this web page, in a non-english language:
Advertisements

2 Responses to “How to create a Password column using the DataGridView”

  1. […] https://csharpdeveloper.wordpress.com/2008/12/19/how-to-create-a-password-column-using-the-datagridvi… […]

  2. Kenny said

    Thanks alot 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: