C# 如何设置DataGridViewTextBoxColumn的密码属性
我使用了C# 如何设置DataGridViewTextBoxColumn的密码属性,c#,datagridview,C#,Datagridview,我使用了DataGridView来实现用户名密码界面。密码显示在DataGridViewTextBoxColumntype列中。如何使用DataGridViewTextBoxColumn的现有代码并实现文本的密码属性 处理EditingControlShowing事件,然后将编辑控件强制转换为TextBox,并手动将UseSystemPasswordChar设置为true: TextBox passwordText = e.Control as TextBox; if (passwordText
DataGridView
来实现用户名密码界面。密码显示在DataGridViewTextBoxColumn
type列中。如何使用DataGridViewTextBoxColumn
的现有代码并实现文本的密码属性 处理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='*'。谢谢你的帮助!也许可以演示如何在Cell_Formatting事件中调用代码,以使答案更完整?
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);
}
}