トップ回答者
DataGridView_CellValidating内でのセル内容のクリア

質問
-
ターチと申します、お世話になります。
VB2008で開発しております。
DataGridViewセルで値をチェックし、もし違ったらセルの内容をクリアし、再度入力できる
状態にしたいと考えております。下記に貼り付けたコードの一行[sender.Item(e.ColumnIndex, e.RowIndex).Value = ""]が動作しません。
[MessageBox.Show]と[e.Cancel = True]の間の行に入れたのは、入力ミスした値を確認したいためです。プログラムの構造に問題があるのは分かっているのですが、経験が浅くいろいろと調べま
したが良い方法が見つかりません、ご教授よろしくお願いします。Private Sub DataGridView1_CellValidating(ByVal sender As DataGridView, _
ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridview1.CellValidating
Dim currentCell As DataGridViewCell = sender.Item(e.ColumnIndex, e.RowIndex)
If currentCell Is Nothing Then
e.Cancel = True
End IfSelect Case currentCell.OwningColumn.Name
Case "Column2"
If currentCell.Value Is Nothing OrElse currentCell.Value Is DBNull.Value Then
Return
End If
If e.FormattedValue.ToString > 10 Then
MessageBox.Show("値が10より大きいです。", "入力エラー", MessageBoxButtons.OK, MessageBoxIcon.Information)
sender.Item(e.ColumnIndex, e.RowIndex).Value = ""
e.Cancel = True
End If
End Select
End Sub
回答
-
VB はよく知らないのでハズレかもしれませんが・・・
DataGridViewCellValidatingEventHandler デリゲートは以下の通り定義されています。
Public Delegate Sub DataGridViewCellValidatingEventHandler ( _
sender As Object, _
e As DataGridViewCellValidatingEventArgs _
)従って、
> Private Sub DataGridView1_CellValidating(ByVal sender As DataGridView, _
の ByVal sender As DataGridView は、ByVal sender As System.Object であるべきなんですが。
ハンドラを自動生成すると ByVal sender As System.Object になるはずですが、自分で書き換えたのでしょ
うか?そこは自動生成されたコード ByVal sender As System.Object のままにしておいて、sender(Object 型)を
DataGridView 型にキャストするようにコードを変更して試したらどうなるでしょうか?- 回答としてマーク ターチ 2009年9月27日 5:05
-
入力を行っているのはEditingControlに対してなので、これに空文字を設定しなければいけません。
Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating Dim currentCell As DataGridViewCell = sender.Item(e.ColumnIndex, e.RowIndex) If currentCell Is Nothing Then e.Cancel = True End If Select Case currentCell.OwningColumn.Name Case "Test2DataGridViewTextBoxColumn" If currentCell.Value Is Nothing OrElse currentCell.Value Is DBNull.Value Then Return End If If e.FormattedValue.ToString > 10 Then MessageBox.Show("値が10より大きいです。", "入力エラー", MessageBoxButtons.OK, MessageBoxIcon.Information) ' sender.Item(e.ColumnIndex, e.RowIndex).Value = "" CType(editingControl, TextBox).Text = "" e.Cancel = True End If End Select End Sub Dim editingControl As Control Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing editingControl = e.Control End Sub
★良い回答には回答済みマークを付けよう! わんくま同盟 MVP - Visual C# http://blogs.wankuma.com/trapemiya/- 回答としてマーク ターチ 2009年9月27日 5:05
すべての返信
-
VB はよく知らないのでハズレかもしれませんが・・・
DataGridViewCellValidatingEventHandler デリゲートは以下の通り定義されています。
Public Delegate Sub DataGridViewCellValidatingEventHandler ( _
sender As Object, _
e As DataGridViewCellValidatingEventArgs _
)従って、
> Private Sub DataGridView1_CellValidating(ByVal sender As DataGridView, _
の ByVal sender As DataGridView は、ByVal sender As System.Object であるべきなんですが。
ハンドラを自動生成すると ByVal sender As System.Object になるはずですが、自分で書き換えたのでしょ
うか?そこは自動生成されたコード ByVal sender As System.Object のままにしておいて、sender(Object 型)を
DataGridView 型にキャストするようにコードを変更して試したらどうなるでしょうか?- 回答としてマーク ターチ 2009年9月27日 5:05
-
入力を行っているのはEditingControlに対してなので、これに空文字を設定しなければいけません。
Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating Dim currentCell As DataGridViewCell = sender.Item(e.ColumnIndex, e.RowIndex) If currentCell Is Nothing Then e.Cancel = True End If Select Case currentCell.OwningColumn.Name Case "Test2DataGridViewTextBoxColumn" If currentCell.Value Is Nothing OrElse currentCell.Value Is DBNull.Value Then Return End If If e.FormattedValue.ToString > 10 Then MessageBox.Show("値が10より大きいです。", "入力エラー", MessageBoxButtons.OK, MessageBoxIcon.Information) ' sender.Item(e.ColumnIndex, e.RowIndex).Value = "" CType(editingControl, TextBox).Text = "" e.Cancel = True End If End Select End Sub Dim editingControl As Control Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing editingControl = e.Control End Sub
★良い回答には回答済みマークを付けよう! わんくま同盟 MVP - Visual C# http://blogs.wankuma.com/trapemiya/- 回答としてマーク ターチ 2009年9月27日 5:05
-
すみません、やっぱりハズレでした。
Option Strict Off の場合は、「遅延バインディング」というのでしょうか、
Private Sub DataGridView1_CellValidating(ByVal sender As DataGridView, ...
も、
Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ...
Dim currentCell As DataGridViewCell = sender.Item(e.ColumnIndex, e.RowIndex)も、コンパイラは通ってしまうんですね。無知でした。(恥)
#でも、やっぱり、個人的には、Option Strict On でないと落ち着かないです。