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ランキング
-
SUN BBSの改造方法
-
wordの数式について 定積分を書...
-
ラジオボタンが両方とも選択で...
-
「value」に2つの値をセットす...
-
<select>タグの幅設定
-
UTF-8で文字化けしないようにす...
-
サーバーのテキストファイルを...
-
チェックボックスの返す値
-
別formのhidden項目を自form値...
-
[HTML]プルダウンメニューの横...
-
OPTIONタグにループは使えない...
-
MSPゴシックで、一番幅を取る文字
-
チェックボックスグループの一...
-
EXCELとの連携
-
チェックボックスでのvalue値の...
-
sendmail 送信先を複数設定す...
-
VB初心者。小数点以下の表示で...
-
VB.net データーグリッドビュー...
-
WEBページを強制的に横画面で見...
-
content-dispositionによるファ...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
SUN BBSの改造方法
-
CGIメールフォームの件名について
-
perlで書いたcgiでsqliteの使い...
-
CGI(Perl)内での必須項目チェ...
-
チェックボックスでのor検索
-
入力フォームに全角・半角スペ...
-
Windows7でVBScriptによるネッ...
-
tracert ができない原因
-
配列をグループ化して出力したい。
-
電話番号を読み込むプログラム...
-
JNAVIから提供されたメー...
-
日本語文字化け(GETメソッド?)
-
文字って数字で表現・・・
-
sendmail -fオプションの指定は...
-
【ASP.NET MVC】フォームヘルパ...
-
sendmailのメール送信ができま...
-
【CGI】画像の表示の仕方【Mini...
-
添付つきメールフォーム:「an ...
-
同じname属性の結果を1行にまと...
-
太郎を太と郎に分ける方法。
おすすめ情報