掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
DataGridViewのRaadOnly背景色を変えない (ID:145903)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
> 氏名セルをクリックするとカーソルがなくなります、個人番号 > にカーソルを残す事はできないでしょうか? 個人番号 = Columns(0)、 氏名セル = Columns(1)、という意味でしょうか。 提示されたコードでは、キャレットは表示されないと思います。 Load (≠Shown)イベントでは、コントロールが表示されていない状態ですから、 Focus メソッドは意味がありません(常に False を返すはず)。 それに、そもそも DataGridView と DataGridView.EditingControl は別物です。 とりあえず今回の件についていえば、EditingControlShowing イベント内にて 「Me.ActiveControl = e.Control」を記述しておけば、事は足りるかと思います。 ところで…。 > DataGridView1.Columns(1).ReadOnly = True > DataGridView1.Focus() > DataGridView1.CurrentCell = DataGridView1(1, 0) > DataGridView1.BeginEdit(True) 1 列目 は ReadOnly なのですよね。にも関わらず、 1 列目のセルを BeginEdit させているのは、何故でしょうか? > If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then And 演算子のかわりに、AndAlso 演算子を使われることをお奨めします。 > DataGridView1.CurrentCell = DataGridView1(1, 0) > DataGridView1.Item(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Yellow 「DataGridView1(列, 行)」表記と 「DataGridView1.Item(列, 行)」表記とが混在しています。 どちらも同じ意味ではありますが、表記は統一しておきましょう。 > Dim no As Integer = DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value 数字以外の値を入力された場合、ここの処理が失敗してしまいます。 Integer.TryParse メソッド等を使って変換するようにするか、もしくは、 DataTable 等をデータバインドして、0 列目に Integer 型の列を 割り当てるようにした方が良いでしょう。 > Select Case no > Case "1" no は Integer 型なのですから、 Case "1" ではなく、Case 1 ですよね。 > DataGridView1.Item(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Yellow Style をセル毎に割り当てると、DataGridViewCellStyle クラスのインスタンスが それぞれに作成されるため、オーバーヘッドが大きくなります。 下記の[セル スタイルの効率的な使用]を参照しておいてください。 http://msdn.microsoft.com/ja-jp/library/ha5xt0d9.aspx 現在編集中のセルを黄色にしたいのであれば、事前に共通のスタイルを用意しておき、 それを割り当てるようにしてみてください。たとえば、DefaultCellStyle 系のプロパティのみを用い、 セル単位での個別スタイルを設定していない場合には、このように記述できます。 Private activeCellStyle As DataGridViewCellStyle Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load activeCellStyle = New DataGridViewCellStyle() activeCellStyle.BackColor = Color.Yellow DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter DataGridView1.AllowUserToAddRows = False DataGridView1.ColumnCount = 2 DataGridView1.RowCount = 100 : DataGridView1.Columns(1).ReadOnly = True End Sub Private Sub DataGridView1_CellBeginEdit(ByVal sender As DataGridView, ByVal e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then sender(e.ColumnIndex, e.RowIndex).Style = activeCellStyle End If End Sub Private Sub DataGridView1_CellEndEdit(ByVal sender As DataGridView, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then sender(e.ColumnIndex, e.RowIndex).Style = Nothing End If End Sub
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.