Display column in DataGridView as password input type

I would like to display a column in a datagridview as a column containing chars passwords. I can not understand why this event is not triggered using 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 me please.

+5
source share
1 answer

You can handle the event EditingControlShowingand then apply the edit control to the TextBox and manually set 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;
    }
    }
}   
+6
source

All Articles