Display column in DataGridView as password input type

10,975

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;
    }
    }
}   
Share:
10,975

Related videos on Youtube

Emil Dumbazu
Author by

Emil Dumbazu

Updated on June 25, 2022

Comments

  • Emil Dumbazu
    Emil Dumbazu almost 2 years

    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.

  • disasterkid
    disasterkid about 9 years
    Correct me if I am wrong but my e does not have a ColumnIndex property.
  • Denis Molodtsov
    Denis Molodtsov almost 9 years
    you could write if(grid.CurrentCell.ColumnIndex == 3 instead of if(e.ColumnIndex == 3)