【问题标题】:How to set Password property for DataGridViewTextBoxColumn如何为 DataGridViewTextBoxColumn 设置密码属性
【发布时间】:2014-05-30 11:22:35
【问题描述】:

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

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

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

    【讨论】:

    • 只要我在文本框中编辑文本,这个就可以工作,我看到密码字符在我移动到另一个单元格时就消失了。请帮忙。
    • 有效!!!我还在 dataGridView1_EditingControlShowing 中设置了 passwordText.PasswordChar = '*'。感谢您的帮助!
    【解决方案2】:
    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);
        }
    }  
    

    【讨论】:

    • 也许展示如何在 Cell_Formatting 事件上调用您的代码以使答案更完整?
    猜你喜欢
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 2022-11-03
    相关资源
    最近更新 更多