Masking password column in datagridview

前端 未结 2 911
遇见更好的自我
遇见更好的自我 2020-12-20 03:31

I\'m having problem with masking the password column. The code below works, but it doesnt work the way I want. While editing it do mask the password but when I am done and c

相关标签:
2条回答
  • 2020-12-20 04:14

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-20 04:33
        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);
        }
    
    0 讨论(0)
提交回复
热议问题