Hi kana250688,
Refer the following code to fing the index of the selected product.
Use ToList method to find the Product and then IndexOf method of BindingSource to get the index value.
Code
Imports System.ComponentModel
Public Class Form1
Private OrderDetail As List(Of OrderDetail)
Private bindingSource As BindingSource = Nothing
Private selectedOrder As String = "TEST 3000"
Private selectedIndex As Integer
Public Sub New()
InitializeComponent()
Me.CenterToScreen()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.LoadData()
End Sub
Private Sub LoadData()
OrderDetail = New List(Of OrderDetail) From {
New OrderDetail() With {
.Invono = "PI0001",
.InvoDate = Convert.ToDateTime("06-05-2024"),
.Days = Convert.ToDateTime("06-05-2024").ToString("dddd"),
.ProductName = "TEST 1000",
.UnitPrice = 15000,
.Quantity = 20
},
New OrderDetail() With {
.Invono = "PI0002",
.InvoDate = Convert.ToDateTime("07-05-2024"),
.Days = Convert.ToDateTime("07-05-2024").ToString("dddd"),
.ProductName = "TEST 2000",
.UnitPrice = 25000,
.Quantity = 20
},
New OrderDetail() With {
.Invono = "PI0003",
.InvoDate = Convert.ToDateTime("06-05-2024"),
.Days = Convert.ToDateTime("06-05-2024").ToString("dddd"),
.ProductName = "TEST 3000",
.UnitPrice = 17000,
.Quantity = 20
},
New OrderDetail() With {
.Invono = "PI0004",
.InvoDate = Convert.ToDateTime("07-05-2024"),
.Days = Convert.ToDateTime("07-05-2024").ToString("dddd"),
.ProductName = "TEST 4000",
.UnitPrice = 18000,
.Quantity = 20
},
New OrderDetail() With {
.Invono = "PI0005",
.InvoDate = Convert.ToDateTime("06-05-2024"),
.Days = Convert.ToDateTime("06-05-2024").ToString("dddd"),
.ProductName = "TEST 5000",
.UnitPrice = 19000,
.Quantity = 20
}
}
bindingSource = New BindingSource With {.DataSource = New BindingList(Of OrderDetail)(CType(OrderDetail, IList(Of OrderDetail)))}
DataGridView1.DataSource = bindingSource
DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
End Sub
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
If (DataGridView1.SelectedRows.Count > 0) Then
selectedOrder = DataGridView1.SelectedRows(0).Cells("ProductName").Value.ToString
End If
End Sub
Private Sub DataGridView1_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
If (Not String.IsNullOrEmpty(selectedOrder) AndAlso (e.ListChangedType = ListChangedType.Reset)) Then
If (bindingSource.List.Count > 0) Then
'error below line code
Dim obj = bindingSource.List.OfType(Of OrderDetail)().ToList().Find(Function(f) f.ProductName = selectedOrder)
selectedIndex = bindingSource.IndexOf(obj)
If (selectedIndex <= 0) Then
selectedIndex = 0
End If
DataGridView1.Rows(selectedIndex).Selected = True
Else
selectedOrder = String.Empty
End If
End If
End Sub
End Class
Public Class OrderDetail
Public Property Invono As String
Public Property InvoDate As DateTime
Public Property Days As String
Public Property ProductName As String
Public Property UnitPrice As Integer
Public Property Quantity As Integer
End Class
Screenshots
Selected Index
Selected DataGridViewRow