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

把Windows的DataGridView的某一列数据显示为"*"

下面的代码把第4列设置为密码列

 第一种方法:

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

 另一种方法:

 

 1         /// <summary>
 2         /// 编辑单元格控件事件
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
 7         {
 8             // 编辑第4列时,把第4列显示为*号
 9             TextBox t = e.Control as TextBox;
10             if (t != null)
11             {
12                 if (this.dataGridView1.CurrentCell.ColumnIndex == 3)
13                     t.PasswordChar = '*';
14                 else
15                     t.PasswordChar = new char();
16             }
17         }

 

 

posted on 2011-06-21 14:53  Lemon_s  阅读(2637)  评论(0)    收藏  举报

导航