VB2010です。
DataGridViewのDataSourceにコレクションを設定しており、
このコレクションのアイテムを指定してDataGridViewに表示されている行インデックスを取得したい。
以下、現状のコードです。Form1にDataGridView、ボタン、テキストボックスを貼り付けており、
テキストボックスに名前を入力してボタンをおすとその行を選択状態とします。
行を取得するために行頭から順に調べておりますが、
アイテムを指定して行を参照できるようなプロパティなり
なにか良い方法が無いでしょうか?
Public Class Form1
Private _persons As Collection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
_persons = New Collection
_persons.Add(New Person With {.Name = "あかい", .Age = 10}, "あかい")
_persons.Add(New Person With {.Name = "いまい", .Age = 13}, "いまい")
_persons.Add(New Person With {.Name = "うかい", .Age = 43}, "うかい")
_persons.Add(New Person With {.Name = "えのき", .Age = 8}, "えのき")
_persons.Add(New Person With {.Name = "おかの", .Age = 3}, "おかの")
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.DataSource = _persons
Dim col As New DataGridViewColumn
col = New DataGridViewTextBoxColumn
col.DataPropertyName = "Name"
col.Name = "NameField"
DataGridView1.Columns.Add(col)
col = New DataGridViewTextBoxColumn
col.DataPropertyName = "Age"
col.Name = "AgeField"
DataGridView1.Columns.Add(col)
End Sub
Private Class Person
Public Property Name As String
Public Property Age As Integer
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name As String = TextBox1.Text
If name.Length = 0 Then Return
If Not _persons.Contains(name) Then Return
Dim person As Person = _persons.Item(name)
For i As Integer = 0 To DataGridView1.Rows.Count - 1
Dim rowItem As Person = DataGridView1.Rows(i).DataBoundItem
If rowItem.Equals(person) Then
DataGridView1.Rows(i).Selected = True
End If
Next
End Sub
End Class
No.5ベストアンサー
- 回答日時:
もう一ついい感じのものがありました。
KeyedCollection クラスです。Private Class Person を Public Class Person に変更して Public Class Form の外に出さなければなりませんが、
Public Class Form1
Private Class Persons
Inherits System.Collections.ObjectModel.KeyedCollection(Of String, Person)
Protected Overrides Function GetKeyForItem(ByVal item As Person) As String
Return item.Name
End Function
Public Overloads Function IndexOf(ByVal name As String) As Integer
Return MyBase.IndexOf(MyBase.Item(name))
End Function
End Class
Private _persons As Persons
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
_persons = New Persons
_persons.Add(New Person With {.Name = "あかい", .Age = 10})
_persons.Add(New Person With {.Name = "いまい", .Age = 13})
_persons.Add(New Person With {.Name = "うかい", .Age = 43})
_persons.Add(New Person With {.Name = "えのき", .Age = 8})
_persons.Add(New Person With {.Name = "おかの", .Age = 3})
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.DataSource = _persons
Dim col As New DataGridViewColumn
col = New DataGridViewTextBoxColumn
col.DataPropertyName = "Name"
col.Name = "NameField"
DataGridView1.Columns.Add(col)
col = New DataGridViewTextBoxColumn
col.DataPropertyName = "Age"
col.Name = "AgeField"
DataGridView1.Columns.Add(col)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name As String = TextBox1.Text
Me.DataGridView1.Rows(_persons.IndexOf(name)).Selected = True
End Sub
End Class
という感じでできます。
参考URL:http://msdn.microsoft.com/ja-jp/library/ms132438 …
No.4
- 回答日時:
Person に Index プロパティーを追加してやれば
Private Sub AddPerson(ByRef p As Person, ByVal key As String)
p.Index = _persons.Count
_persons.Add(p, key)
End Sub
でコレクションに追加して
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim p As Person = _persons.Item(Me.TextBox1.Text)
Me.DataGridView1.Rows(p.Index).Selected = True
End Sub
とできます。
Person に Index プロパティーを追加するというのが設計思想に反するかもしれませんが…。
No.3
- 回答日時:
すいません答えじゃないですが...
簡単なのは、CellFormattingイベントを使う。
ボタンを押した時に、それぞれセルのvalueにテキストボックスに入力されている値が
含まれているかInstr関数で判定。
含まれていれば、
.DefaultCellStyle.BackColorや
.DefaultCellStyle.ForeColorを変更するとか...
ただ、行番号(インデックス)のみを取得したいのであれば、
やはり、全行を確認するしかないのではないでしょうか...
No.1
- 回答日時:
datagridviewに主キーとなる列があるのなら...
もしくは、テキストボックスに入力する値が、他にはない場合...
'全行を比較
For Earc v As DatagridviewRow In Me.Datagridview.Rows
'テキストと列("名前")の値が同じならその行を選択状態にし、カレント行とする。
If Me.テキストボックス.Text = v.Cells("名前").Value Then
Me.datagridview.CurrentCell = v.Cells("名前")
v.Selected = True
Exit For
End If
Next
ただし、MultiSelectがTrueの場合は一旦datagridviewのカレント行を以下のように無くす必要があります。
Me.datagridview.CurrentCell = Nothing
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- Visual Basic(VBA) 【Excel VBA】自動メール送信の機能追加 5 2022/09/29 12:53
- Visual Basic(VBA) EXCEL VBAにて動的にCheckBOXを複数作成し、同BOXにイベントを追加したい 1 2023/03/16 07:05
- Visual Basic(VBA) ①ExcelVBAでカレンダーを作り、別のユザーフォームで日付を入力したいのですがエラーになります。 1 2023/02/17 18:39
- Excel(エクセル) VBAの指示の内容 昨日こちらでご教示頂いたのですが初心者な為、一つ一つの指示が何をやっているのかわ 2 2022/10/25 18:08
- Visual Basic(VBA) 別シートから年齢別の件数をカウントしたいの続き 5 2023/01/24 00:16
- Visual Basic(VBA) VBAのユーザーフォームのテキストボックスに入力制限をしたい 6 2022/11/15 08:28
- Visual Basic(VBA) 【VBA】写真の貼り付けコードがうまく機能しません。 5 2022/09/01 18:43
- C言語・C++・C# C# DatagridviewにExcelシートを反映するとエラーが出る 2 2023/05/06 17:12
- Visual Basic(VBA) VBA処理追加 こちらでご教示頂いたのですが回答完了させてしまいましたのでこちらからまた質問させてく 2 2022/10/27 09:57
- Visual Basic(VBA) excel vbaでvlooupの変数がわかりません。 7 2022/05/30 09:35
関連するカテゴリからQ&Aを探す
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
チェックボックスでのor検索
-
sendmailのメール送信ができま...
-
@town = ("Aタウン","Bタウン",...
-
strlen
-
【CGI】画像の表示の仕方【Mini...
-
wordの数式について 定積分を書...
-
ラジオボタンが両方とも選択で...
-
FORMのselectの選択肢を最初か...
-
「value」に2つの値をセットす...
-
perlで重複データを集計
-
NNでうまく動きません
-
select値をhiddenのvalueに渡し...
-
UTF-8で文字化けしないようにす...
-
「利用規約の同意」チェックボ...
-
OPTIONタグにループは使えない...
-
OBJECTタグで、PARAMを使用する...
-
表示を一覧表形式にしたいので...
-
選択式アンケート項目合計点で...
-
サーバーのテキストファイルを...
-
<html>perlスクリプト</html>
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
perlで書いたcgiでsqliteの使い...
-
SUN BBSの改造方法
-
tracert ができない原因
-
日本語文字化け(GETメソッド?)
-
チェックボックスでのor検索
-
入力フォームに全角・半角スペ...
-
ネットワークサービスについて
-
【ASP.NET MVC】フォームヘルパ...
-
【CGI】画像の表示の仕方【Mini...
-
DataGridViewの行取得
-
CGI.pmを利用のテーブル表示に...
-
掲示板スパム対策(改造)ガー...
-
ファイル名変更プログラム
-
CGIで合計を書き出す方法
-
同じname属性の結果を1行にまと...
-
禁止ワードの設定
-
フォーム送信でログに保存
-
Windows7でVBScriptによるネッ...
-
perl ヒア文(print <<"EOM" ...
-
Webアプリケーションを作りたい!
おすすめ情報