.Net(VB、C#)のDataGridViewでベリファイ入力(2回入力)を実現する
.Net(VB、C#)のDataGridViewでデータエントリー業務に使えそうなエントリー(1回目)とベリファイ(2回目)の2回入力で入力項目を確定させる方法。
以下は、サンプル。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | Public Class Form1 Public comparisonSource As String = String.Empty Private Sub dataGridView1_CellValidating(sender As System.Object, e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dataGridView1.CellValidating Dim dgv As DataGridView = DirectCast(sender, DataGridView) If comparisonSource.Length = 0 Then If Me.Validate() Then comparisonSource = e.FormattedValue.ToString() textBox1.Text = comparisonSource dgv.CancelEdit() e.Cancel = True Else comparisonSource = e.FormattedValue.ToString() textBox1.Text = comparisonSource dgv.CancelEdit() End If Else If comparisonSource <> e.FormattedValue.ToString() Then comparisonSource = e.FormattedValue.ToString() textBox1.Text = comparisonSource dgv.Rows(e.RowIndex).ErrorText = "入力値が異なります。" dgv.CancelEdit() e.Cancel = True Else comparisonSource = String.Empty End If End If End Sub Private Sub dataGridView1_CellValidated(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dataGridView1.CellValidated Dim dgv As DataGridView = DirectCast(sender, DataGridView) dgv.Rows(e.RowIndex).ErrorText = Nothing End Sub End Class |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace CSTest001 { public partial class Form1 : Form { public string comparisonSource = string.Empty; public Form1() { InitializeComponent(); } private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { DataGridView dgv = (DataGridView)sender; if (comparisonSource.Length == 0) { if (this.Validate()) { comparisonSource = e.FormattedValue.ToString(); textBox1.Text = comparisonSource; dgv.CancelEdit(); e.Cancel = true; } else { comparisonSource = e.FormattedValue.ToString(); textBox1.Text = comparisonSource; dgv.CancelEdit(); } } else { if (comparisonSource != e.FormattedValue.ToString()) { comparisonSource = e.FormattedValue.ToString(); textBox1.Text = comparisonSource; dgv.Rows[e.RowIndex].ErrorText = "入力値が異なります。"; dgv.CancelEdit(); e.Cancel = true; } else { comparisonSource = string.Empty; } } } private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e) { DataGridView dgv = (DataGridView)sender; dgv.Rows[e.RowIndex].ErrorText = null; } } } |