Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display column in DataGridView as password input type

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.

like image 784
Emil Dumbazu Avatar asked Sep 25 '12 17:09

Emil Dumbazu


1 Answers

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;
    }
    }
}   
like image 190
Aghilas Yakoub Avatar answered Oct 02 '22 01:10

Aghilas Yakoub