1

Is there a way to replace the characters entered in a cell of a DataGridView with an asterisk while entering them? If so, how can I do this? Any help is appreciated.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574

2 Answers2

3

As Mark Rideout writes in his answer found on social.msdn.microsoft.com

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

TextBox t = e.Control as TextBox;
if (t != null)
{
    t.UseSystemPasswordChar = true;
}
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
0

try this

in the DataGridView EditingControlShowing event

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control.GetType() == typeof(DataGridViewTextBoxEditingControl))
            {
                TextBox txt = (TextBox)e.Control;
                txt.PasswordChar = '*';
            }
        }

and in the Cell Formating Event

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if(e.Value != null)
                e.Value = new String('*', e.Value.ToString().Length);
        }

hope this helps

Binil
  • 6,445
  • 3
  • 30
  • 40
  • 1
    Why in the world would you try to do this manually when you can set the `UseSystemPasswordChar` property to "True" and let the system handle it for you automatically? The examples you've provided don't even make sense together—why bother setting the `PasswordChar` if you're going to hardcode an asterisk in the `CellFormatting` event handler method anyway? – Cody Gray - on strike Jan 04 '11 at 06:31