1

我有一个数据网格视图控件。现在我有两个文本框列。其中第一个我设置为密码。问题是,每当我尝试在另一个文本框中编辑某些内容时,它同样会显示在蒙版文本中。如何避免这种情况?

我的代码如下

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{


   if (dataGridView1.CurrentCell.ColumnIndex == 1)
  {
      TextBox tb = e.Control as TextBox;
      if (tb != null)
      {
          tb.PasswordChar = '*';
      }
  } 
}

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
   if (e.ColumnIndex == 1 && e.Value != null)
   {

     e.Value = new string('*', e.Value.ToString().Length);

   }
}
4

1 回答 1

1

这是解决方案:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  {

        TextBox tb = e.Control as TextBox;
        if (tb != null)
        {
           if (dataGridView1.CurrentCell.ColumnIndex == 1)
           {
              tb.PasswordChar = '*';
           }
           else
           {
              tb.PasswordChar = (char)0;
           }
     } 
  }

解释很快就会到来。我现在只能说tb.PasswordChar所有textBox. 仍在检查为什么

在这里找到了解释。基本上“DataGridView 控件一次承载一个编辑控件”,因此更改PasswordChar一个单元格的属性就是为整个DataGrid. 研究这个非常有趣。谢谢

于 2013-07-08T06:35:26.637 回答