Masking password column in datagridview

萝らか妹 提交于 2019-12-02 08:02:38
    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if ((e.ColumnIndex == 5 || e.ColumnIndex == 10) && e.Value != null)
            {
                dataGridView1.Rows[e.RowIndex].Tag = e.Value;
                e.Value = new String('\u25CF', e.Value.ToString().Length);
            }
    }

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (dataGridView1.CurrentCell.ColumnIndex == 5 || dataGridView1.CurrentCell.ColumnIndex == 10)//select target column
        {
            TextBox textBox = e.Control as TextBox;
            if (textBox != null)
            {
                textBox.UseSystemPasswordChar = true;
            }
        }
        else
        {
            TextBox textBox = e.Control as TextBox;
            if (textBox != null)
            {
                textBox.UseSystemPasswordChar = false;
            }
        }
        var txtBox = e.Control as TextBox;
        txtBox.KeyDown -= new KeyEventHandler(underlyingTextBox_KeyDown);
        txtBox.KeyDown += new KeyEventHandler(underlyingTextBox_KeyDown);
    }

Assuming the the name of your DataGridView is dataGridView1 and the password column is 1, add this to CellFormatting:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 1 && e.Value != null)
    {
        e.Value = new String('*', e.Value.ToString().Length);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!