プロが教える店舗&オフィスのセキュリティ対策術

DataGridViewの件で質問したいのですが。
よろしくお願いします。


VB2005で、DataGridViewの特定のセルでカーソルがとどまる(選択状態)ようにしたいのですが
思ったように動いてくれません。例えば、下のようなDataGtridViewがあったとして,


------------------
|ID | X | Y |
------------------
|001| 100 | |
------------------
|002| 150 | |
------------------
|003| 105 | |
------------------

ID が002の行で、Y列のセルにカーソルが入りXと違う数を入力したら、メッセージを
表示させて、そのセルにカーソルがとどまる処理を作りたいのですが。下の行のセルに
移動してしまいます。

 DataGridView_CellEndEditイベントやDataGridView1_CellValueChangedなどで
カーソルがとどまる処理をしているつもりなのです下の行に移動してしまいます。
DataGridViewの初期設定の問題でしょうか?

Private Sub DataGridView1_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged

Dim DgvRow As Integer
'行をセット
DgvRow = e.RowIndex
' X と Yが異なれば、メッセージ表示
If DataGridView1("X", e.RowIndex).Value <> DataGridView1("Y", e.RowIndex).Value Then
MsgBox("X <> Y", MsgBoxStyle.OkOnly)
Me.DataGridView1.CurrentCell = Me.DataGridView1("Y", DgvRow)
' これも駄目だった ↓
'Me.DataGridView1.CurrentCell = Me.DataGridView1("Y", e.RowIndex - 1)
End If
End Sub

アドバイスいただけたら幸いです。よろしくお願い致します。

開発環境
VB2005
Visual Studio 2005 Standard Edition(SP1)
Windows XP Pro SP2

A 回答 (1件)

CellValidatingイベントハンドラで処理するといいと思います。



If e.ColumnIndex <> DataGridView1.Columns("Y").Index Then Exit Sub
If DataGridView1("X", e.RowIndex).Value IsNot Nothing AndAlso _
DataGridView1.EditingControl IsNot Nothing Then
If Not DataGridView1("X", e.RowIndex).Value.ToString.Equals(DataGridView1.EditingControl.Text) Then
MsgBox("X <> Y", MsgBoxStyle.OkOnly)
e.Cancel = True
End If
End If
    • good
    • 0
この回答へのお礼

この辺のDataGridViewのセルの処理は複雑みたいですね。
ありがとうございます。参考になりました。

お礼日時:2008/05/07 08:41

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!