いつもお世話になっております。
VB2008を使い開発してます、いろいろ調べました解決できません、
ご教授よろしくお願いいたします。
フォームにデータグリッドビューとボタンを貼り付けて、会議の出席状況を管理
するプログラムを作っています。
届順、番号、氏名を表示し、届順に番号入力することにより氏名を表示し、出席
以外の場合は氏名(状況)欄に理由を入力します、届順欄は初期値をReadOnlyにし
クリックされた時にキャレット表示されなくなる為、SelectionChangedイベント
で強制的に番号欄にフォーカスを移動しています。
問題の現象はボタンをクリック後、グリットビューに表示する際に最初の行にある
氏名(状況)欄の佐藤 (欠席)が佐藤のみの表示になってしまう事です、デバッグで
確認してみるとsender.CurrentCellの後にCellValidatedが動いてしまい氏名が
再表示されているのが分かりました、SelectionChangedイベントが原因なのです...
会議の出席届順がボタンクリック直後の結果と同じ場合は、直接氏名(状況)欄にマウス
で移動し修正したいのです、ボタンクリック時のDataGridView1.CurrentCell = Nothing
をコメントする事により正常に初期表示されるようになったのですが、グリッドビュー
の(1,0)が青く反転してしまい、別のセルに移動すると氏名(状況)欄の表示が変わって
しまいます、実際には先頭行にカーソルが移動してるようです。
(1,0)が青く反転せずに、ボタンクリック後グリッドビューにフォーカスが移動しない
ような方法はないでしょうか?
Public Class Form1
Private dt As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.DataSource = dt
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dt.Columns.Clear()
dt.Rows.Clear()
dt.Columns.Add("届順")
dt.Columns.Add("番号")
dt.Columns.Add("氏名(状況)")
dt.Rows.Add("1", "1", "佐藤 (欠席)")
dt.Rows.Add("2", "2", "鈴木")
dt.Rows.Add("3", "3", "高橋")
dt.Rows.Add("4", "4", "田中 (遅れる)")
dt.Rows.Add("5", "5", "渡辺")
DataGridView1.Columns(0).ReadOnly = True
DataGridView1.AllowUserToAddRows = False
DataGridView1.CurrentCell = Nothing
End Sub
Private Sub DataGridView1_CellValidating(ByVal sender As DataGridView, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
Dim currentCell As DataGridViewCell = sender.CurrentCell
If currentCell Is Nothing OrElse currentCell.Value Is DBNull.Value Then
Return
End If
If e.ColumnIndex = 1 Then
If e.FormattedValue.ToString < "1" OrElse e.FormattedValue.ToString > "5" Then
MsgBox("入力エラー")
e.Cancel = True
End If
End If
End Sub
Private Sub DataGridView1_CellValidated(ByVal sender As DataGridView, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValidated
Dim currentCell As DataGridViewCell = sender.CurrentCell
If currentCell Is Nothing OrElse currentCell.Value Is DBNull.Value Then
Return
End If
If e.ColumnIndex = 1 Then
If currentCell.Value = "1" Then
sender.Item(e.ColumnIndex + 1, e.RowIndex).Value = "佐藤"
ElseIf currentCell.Value = "2" Then
sender.Item(e.ColumnIndex + 1, e.RowIndex).Value = "鈴木"
ElseIf currentCell.Value = "3" Then
sender.Item(e.ColumnIndex + 1, e.RowIndex).Value = "高橋"
ElseIf currentCell.Value = "4" Then
sender.Item(e.ColumnIndex + 1, e.RowIndex).Value = "田中"
ElseIf currentCell.Value = "5" Then
sender.Item(e.ColumnIndex + 1, e.RowIndex).Value = "渡辺"
End If
End If
End Sub
Private Sub DataGridView1_SelectionChanged(ByVal sender As DataGridView, ByVal e As EventArgs) Handles DataGridView1.SelectionChanged
Dim pos As Point = sender.CurrentCellAddress
If pos.X = 0 AndAlso pos.Y >= 0 Then
sender.CurrentCell = sender.Item(1, pos.Y)
End If
End Sub
End Class
ツイート | ![]() |