如何把Windows 窗体 DataGridView 的某一列的数据显示为“*”。
/// <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 = string.Empty.PadLeft(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();
}
}
/// 单元格显示格式事件
/// </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 = string.Empty.PadLeft(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();
}
}
记住该记住的,忘记该忘记的,改变能改变的,接受不能改变的!