Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 continue to the next datagridviewcell password becomes visible.

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;
            }                
        }
        var txtBox = e.Control as TextBox;
        txtBox.KeyDown -= new KeyEventHandler(underlyingTextBox_KeyDown);
        txtBox.KeyDown += new KeyEventHandler(underlyingTextBox_KeyDown);
}

Also in edit mode it should have mask only the columns with index 5 && 10 however it masks all columns. I cannot solve these issues, any help would be great.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
668 views
Welcome To Ask or Share your Answers For Others

1 Answer

    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);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...