DataGridView 密码列(显示为*号)的设置
曾經為在DataGridView中設置密碼列(顯示為*號)而發愁,如何把Windows 窗體 DataGridView 的某一列的數據顯示為“*”。
哈哈,今天終于搞定了。需要在DataGridView的2個事件中寫代碼真麻煩!下面的代碼把第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();
??????????? }
??????? }
總結
以上是生活随笔為你收集整理的DataGridView 密码列(显示为*号)的设置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 平板拖车一般多少钱
- 下一篇: 自定义GridView分页模板