Masking password column in datagridview
Asked Answered
A

2

6

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.

Ahrens answered 14/7, 2015 at 7:21 Comment(5)
Have you tried by changing(Textbox as Password in Properties of Textbox) in UI ?Homology
Can I do that by code, because actually there is no textbox, it is datagridview cell's textBox. I dont know how to change textbox's propertis from design view.Ahrens
Try this one: #23953925Rhamnaceous
I tried that one already this is not what I exactly try to do. I'm trying that while typing the password characters must be shown as passwordchar like " * ". However in that case after you done with the cell program masks the password. @NamBìnhAhrens
Sorry for misunderstanding your question.Rhamnaceous
E
10
    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);
    }
Esperanzaespial answered 14/7, 2015 at 9:2 Comment(1)
else { textBox.UseSystemPasswordChar = false; } This is a good trick, it works now thank you.Ahrens
H
8

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);
    }
}
Headachy answered 28/6, 2016 at 10:53 Comment(2)
Thank you for your solutionMalayoindonesian
In newer version of the DataGridView control, cell values are automatically shown as tooltips when cursor moves on them. To ultimately hide values through these tooltips, set tooltips off using command grid.ShowCellToolTips = false;Simson

© 2022 - 2024 — McMap. All rights reserved.