DataGridView 密码列(显示为*号)的设置
<转自大豆男生>
代码
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 }
17
18 /// <summary>
19 /// 编辑单元格控件事件
20 /// </summary>
21 /// <param name="sender"></param>
22 /// <param name="e"></param>
23 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
24 {
25 // 编辑第4列时,把第4列显示为*号
26 TextBox t = e.Control as TextBox;
27 if (t != null)
28 {
29 if (this.dataGridView1.CurrentCell.ColumnIndex == 3)
30 t.PasswordChar = '*';
31 else
32 t.PasswordChar = new char();
33 }
34 }