6

I would like to display a column in a datagridview as a column which contains password chars.I cannot figure it out why does this event is not triggered by the datagridview.

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

Help please.

4
  • Have you tried rewiring the event? Please check if it works when bind this from first again. Commented Sep 25, 2012 at 18:50
  • @arjunshetty2020 i don't understand what you mean by bind this from first again .. Commented Sep 25, 2012 at 19:01
  • sorry about that. I meant delete the event and create it from first again Commented Sep 26, 2012 at 5:01
  • Your solution worked perfectly for me with no changes!
    – Nate S.
    Commented Nov 12, 2014 at 15:04

1 Answer 1

7

You can handle the EditingControlShowing event and then cast the editing control to a TextBox and manually set the UseSystemPasswordChar to true.

private void dataGridView1_EditingControlShowing(object sender, 
    DataGridViewEditingControlShowingEventArgs e)
{
    if(e.ColumnIndex == 3)//select target column
    {
    TextBox textBox = e.Control as TextBox;
    if (textBox != null)
    {
        textBox.UseSystemPasswordChar = true;
    }
    }
}   
2
  • Correct me if I am wrong but my e does not have a ColumnIndex property. Commented Mar 4, 2015 at 13:44
  • 2
    you could write if(grid.CurrentCell.ColumnIndex == 3 instead of if(e.ColumnIndex == 3) Commented Jun 2, 2015 at 10:38

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