【VB.NET】DataGridViewのセル選択時に詳細情報を別のコントロールに表示する

画面イメージ

フォーカス移動時、ラベルに詳細情報を設定する

VB
 Private Sub DataGridView1_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEnter

     Dim cell = DataGridView1.Rows(e.RowIndex).Cells("CustomerID").Value
     Dim row = G_dtCustomer.AsEnumerable.Where(Function(dr) dr("CustomerID") = cell).FirstOrDefault

     lblName.Text = row("Name")
     lblTellNo.Text = row("TellNo")

 End Sub

動作確認用コード

VB
 Public G_dtReservation As New DataTable
 Public G_dtCustomer As New DataTable
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

     With G_dtReservation
         .Columns.Add("CustomerID", GetType(String))
         .Columns.Add("Reservation", GetType(String))
         .Rows.Add("1", "2024/05/06")
         .Rows.Add("2", "2024/05/20")
         .Rows.Add("3", "2024/06/01")
         .Rows.Add("4", "2024/06/03")
     End With

     With G_dtCustomer
         .Columns.Add("CustomerID", GetType(String))
         .Columns.Add("Name", GetType(String))
         .Columns.Add("TellNo", GetType(String))
         .Rows.Add("1", "山田太郎", "080-1111-2222")
         .Rows.Add("2", "山田花子", "080-2222-3333")
         .Rows.Add("3", "山田次郎", "080-3333-4444")
         .Rows.Add("4", "山田三郎", "080-4444-5555")
     End With

     With DataGridView1
         .DataSource = G_dtReservation
         .AllowUserToAddRows = False
         .Columns("CustomerID").ReadOnly = True
         .Columns("Reservation").ReadOnly = True
     End With

 End Sub

コメント

タイトルとURLをコピーしました