如何设置DataGridViewTextBoxColumn的Password属性 [英] How to set Password property for DataGridViewTextBoxColumn

查看:214
本文介绍了如何设置DataGridViewTextBoxColumn的Password属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have used DataGridView to implement username-password UI. The passwords are shown in DataGridViewTextBoxColumn type column. How can I use the existing code for DataGridViewTextBoxColumn and implement password property for the text?

推荐答案

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

Handle the EditingControlShowing event and then cast the editing control to a TextBox and manually set the UseSystemPasswordChar to true:

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



编辑



你可以试试这个:

Edit

Could you try this :

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

这篇关于如何设置DataGridViewTextBoxColumn的Password属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆