【问题标题】:How to create column of type password in gridview?如何在gridview中创建密码类型的列?
【发布时间】:2011-02-13 17:10:44
【问题描述】:

我正在创建一个应用程序,用户在该应用程序中选择文件并提供打开该文件的凭据。为此,我在 gridview 中创建了三列。
用户在密码栏中输入密码。
我想显示 * 来代替字符,例如我们可以创建密码类型的文本框。
我在GridView_CellClick 事件上尝试过这段代码:

if (GridView.Columns[e.ColumnIndex].HeaderText == "Password")
{ 
   txtPassword[e.RowIndex] = new TextBox();
   txtPassword[e.RowIndex].Name = "txtPassword"+e.RowIndex;
   txtPassword[e.RowIndex].PasswordChar = '*';
   txtPassword[e.RowIndex].Visible = true;
   txtPassword[e.RowIndex].TextChanged += new      

   if (GridView.CurrentCell.Value == null)
      txtPassword[e.RowIndex].Text = "";
   else
      txtPassword[e.RowIndex].Text = GridView.CurrentCell.Value.ToString();

   txtPassword[e.RowIndex].Location = GridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex + 1, false).Location;

   txtPassword[e.RowIndex].Size = GridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex + 1, false).Size;

   txtPassword[e.RowIndex].Visible = true;
   txtPassword[e.RowIndex].Focus();
} 

但在上述解决方案中显示字符。
我该如何解决这个问题?

【问题讨论】:

    标签: c# winforms gridview passwords


    【解决方案1】:

    您可以创建自定义单元格和列,并使用带有密码掩码的文本框作为您的编辑控件。

    为了避免在离开编辑模式时返回明文,您可以将实际密码值存储在单元格的单独属性中,并在 GetFormattedValue 事件中(我相信)您可以返回一个完全由“ *" 字符来屏蔽常规显示。

    Protected Overrides Function GetFormattedValue(ByVal value As Object, ByVal rowIndex As Integer, ByRef cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal valueTypeConverter As System.ComponentModel.TypeConverter, ByVal formattedValueTypeConverter As System.ComponentModel.TypeConverter, ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object
            Dim strVal As String
    
            strVal = New String(CChar("*"), value.ToString.Length)
    
            Return MyBase.GetFormattedValue(strVal, rowIndex, cellStyle, valueTypeConverter, formattedValueTypeConverter, context)
    
        End Function
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
        ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
    
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
                                            dataGridViewCellStyle)
    
            Dim ctl As PasswordTextBoxEditingControl = _
                CType(DataGridView.EditingControl, PasswordTextBoxEditingControl)
    
            If IsDBNull(Me.Value) Then
                ctl.Text = ""
    
    
            Else
                ctl.Text = CType(Me.Value, String)
                ctl.PasswordChar = "*"
                ctl.Mask = "*"
            End If
    
        End Sub
    

    有关您尝试执行的操作的更多信息,请访问: http://www.vbforums.com/showthread.php?t=554744

    【讨论】:

      【解决方案2】:

      我认为它应该可以处理 EditingControlShowing 事件并在处理程序中添加以下代码:

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

      CellFormatting 事件处理代码:

      if(e.ColumnIndex = 2)
      {
          if(e.Value != null)
          {
              e.Value = New string("*", e.Value.ToString().Length);
          }
      }
      

      在这种情况下 e 应该有一个 ColumnIndex 属性:)

      【讨论】:

      • EditingControlShowing 的处理程序默认将DataGridViewEditingControlShowingEventArgs e 作为第二个参数,就是这个。
      • columnindex 不是 DataGridViewEditingControlShowingEventArgs 的成员。可以验证吗?
      • 对不起,把事件搞混了,试试dataGridView1.CurrentCell.ColumnIndex
      • 现在它可以工作了..但是当我转移到其他行时。它再次重置为原始文本:(
      • 为此您还必须处理 CellFormatting 事件,我将用示例更新我的答案。
      【解决方案3】:

      一种解决方案是创建您自己的单元格类型并将该类型分配给您的密码列。例如,您将只添加标准的 TextBoxPassword 到它,然后它应该可以按您的意愿工作。

      MSDN 上有更详细的说明和可用的源代码。

      你只需要创建你喜欢的CellTemplate

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-13
        • 2023-03-13
        • 2011-01-29
        • 2013-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多