
/// Copyright(c) 2016 pakkin. All Rights Reserved.
/// [改訂履歴]
/// 2016.08.30 作成
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private const int COL_PWD_DMY = 0; // DataGridViewの1列目:表示用パスワード(ダミー)
private const int COL_PWD_HID = 1; // DataGridViewの2列目:非表示パスワード(真)
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (e.Control is DataGridViewTextBoxEditingControl)
{
DataGridViewTextBoxEditingControl txt = (DataGridViewTextBoxEditingControl)e.Control;
//txt.UseSystemPasswordChar = !this.chkShowPassword.Checked && dgv.CurrentCell.ColumnIndex == COL_PWD_DMY;
txt.PasswordChar = !this.chkShowPassword.Checked && dgv.CurrentCell.ColumnIndex == COL_PWD_DMY ? '*' : '\0';
}
}
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (!this.chkShowPassword.Checked && dgv.CurrentCell.ColumnIndex == COL_PWD_DMY)
{
DataGridViewRow row = dgv.Rows[dgv.CurrentCell.RowIndex];
dgv.CurrentCell.Value = row.Cells[COL_PWD_HID].Value;
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.CurrentCell.ColumnIndex == COL_PWD_DMY)
{
DataGridViewRow row = dgv.Rows[dgv.CurrentCell.RowIndex];
row.Cells[COL_PWD_HID].Value = dgv.CurrentCell.Value;
if (!this.chkShowPassword.Checked)
{
dgv.CurrentCell.Value = new String('*', dgv.CurrentCell.Value.ToString().Length);
}
}
}
private void chkShowPassword_CheckedChanged(object sender, EventArgs e)
{
DataGridView dgv = this.dataGridView1;
foreach (DataGridViewRow row in dgv.Rows)
{
if (this.chkShowPassword.Checked)
{
row.Cells[COL_PWD_DMY].Value = row.Cells[COL_PWD_HID].Value;
}
else if (row.Cells[COL_PWD_DMY].Value == null)
{
row.Cells[COL_PWD_HID].Value = null;
}
else
{
row.Cells[COL_PWD_HID].Value = row.Cells[COL_PWD_DMY].Value;
row.Cells[COL_PWD_DMY].Value = new String('*', row.Cells[COL_PWD_DMY].Value.ToString().Length);
}
}
}
}
}
Copyright(c) 2014-2022 pakkin. All Rights Reserved.