How to set Password property for DataGridViewTextBoxColumn

c#, datagridview

Solution

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

TextBox passwordText = e.Control as TextBox;
if (passwordText != null)
{
    passwordText.UseSystemPasswordChar = true;
}

Edit

Could you try this :

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == “passwordDataGridViewTextBoxColumn” && e.Value != null)
    {
        dataGridView1.Rows[e.RowIndex].Tag = e.Value;
        e.Value = new String(‘*’, e.Value.ToString().Length);
    }
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (dataGridView1.CurrentRow.Tag != null)
        e.Control.Text = dataGridView1.CurrentRow.Tag.ToString();
}

Problem

I have used `DataGridView` to implement username-password UI. The passwords are shown in `DataGridViewTextBoxColumn` type column. How can I use the existing code for `DataGridViewTextBoxColumn` and implement password property for the text?

Original source