屏蔽datagridview中的密码列

时间:2015-07-14 07:21:34

标签: c# winforms passwords datagridviewcolumn datagridtextcolumn

我在屏蔽密码列时遇到问题。下面的代码有效,但它不能按我想要的方式工作。编辑它时会屏蔽密码但是当我完成后继续下一个datagridviewcell密码变得可见。

public class ErrorController : Controller
{
    public ActionResult NotFound()
    {
        Response.StatusCode = 404;
        return View();
    }
}

同样在编辑模式下,它应该只有掩码索引为5&&的列。但是它掩盖了所有列。 我无法解决这些问题,任何帮助都会很棒。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

假设您的DataGridView的名称是dataGridView1且密码列是1,请将其添加到CellFormatting:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 1 && e.Value != null)
    {
        e.Value = new String('*', e.Value.ToString().Length);
    }
}