DataGridView 密码列(显示为*号)的设置

本文介绍了如何在DataGridView中设置一列显示为密码格式,即输入的内容以*号代替,同时在编辑时依然保持*号显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

///DataGridView 的某一列的数据显示为“*”。---DataGridView中设置密码列(显示为*号)
  ///把第4列设置为密码列(显示为*号):

 /// <summary>
        
/// 单元格显示格式事件
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            
// 把第4列显示*号,*号的个数和实际数据的长度相同
            if (e.ColumnIndex == 3)
            {
                
if (e.Value != null && e.Value.ToString().Length > 0)
                {
                    e.Value 
= new string('*',e.Value.ToString().Length);
                }
            }
        }

        
/// <summary>
        
/// 编辑单元格控件事件
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            
// 编辑第4列时,把第4列显示为*号
            TextBox t = e.Control as TextBox;
            
if (t != null)
            {
                
if (this.dataGridView1.CurrentCell.ColumnIndex == 3)
                    t.PasswordChar 
= '*';
                
else
                    t.PasswordChar 
= new char();
            }
        }

 

 

'vb.net code:

Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        If e.ColumnIndex = 3 Then
            If e.Value = "" And e.Value.ToString.Length > 0 Then
                e.Value = New String("*", e.Value.ToString.Length)
            End If
        End If

    End Sub

    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        Dim t As TextBox
        t = e.Control

        If Not t Is Nothing Then
            If Me.DataGridView1.CurrentCell.ColumnIndex = 3 Then
                t.PasswordChar = "*"
            Else
                t.PasswordChar = New Char()
            End If
        End If
    End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值