我使用DataGridView
来实现用户名密码UI。密码显示在DataGridViewTextBoxColumn
类型列中。如何使用DataGridViewTextBoxColumn
的现有代码并为文本实现密码属性?
答案 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);
}
}