如何为DataGridViewTextBoxColumn设置Password属性

时间:2014-05-30 11:22:35

标签: c# datagridview

我使用DataGridView来实现用户名密码UI。密码显示在DataGridViewTextBoxColumn类型列中。如何使用DataGridViewTextBoxColumn的现有代码并为文本实现密码属性?

2 个答案:

答案 0 :(得分:7)

处理EditingControlShowing事件,然后将编辑控件转换为TextBox并手动将UseSystemPasswordChar设置为true:

TextBox passwordText = e.Control as TextBox;
if (passwordText != null)
{
    passwordText.UseSystemPasswordChar = true;
}

修改

你可以试试这个:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == “passwordDataGridViewTextBoxColumn” && e.Value != null)
    {
        dataGridView1.Rows[e.RowIndex].Tag = e.Value;
        e.Value = new String(‘*’, e.Value.ToString().Length);
    }
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (dataGridView1.CurrentRow.Tag != null)
        e.Control.Text = dataGridView1.CurrentRow.Tag.ToString();
}

答案 1 :(得分:-1)

if (e.ColumnIndex >= 0)
{
    if (dataGridView.Columns[e.ColumnIndex].Name == "Password" && e.Value != null)
    {
        dataGridView.Rows[e.RowIndex].Tag = e.Value;
        e.Value = new String('\u2022', e.Value.ToString().Length);
    }
}