Personaly I think you are going about this the wrong way. I would create a class LineItem with three properties LineID, LineCode (which would be an Enum) and LineDescription. This Class would have a shared Function which would return a list of all current line items from the Database. Something like this:
Enum LineCode
Code1
Code2
Code3
End Enum
Class LineItem
Public Property LineID As Integer
Public Property LineCode As LineCode
Public Property LineDescription As String
Public Shared Function GetCurrentLineItems() As List(Of LineItem)
Dim lineItems As List(Of LineItem) = New List(Of LineItem)
Using con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source = D:\Documents\LineItems.mdb")
Dim cmd As New OleDb.OleDbCommand("SELECT LineID, LineCode, LineDescription FROM LineItems Order By LineID", con)
con.Open()
Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader()
While dr.Read()
Dim newLine As New LineItem()
newLine.LineID = dr.GetInt32(0)
newLine.LineCode = dr.GetInt32(1)
newLine.LineDescription = dr.GetString(2)
lineItems.Add(newLine)
End While
dr.Close()
End Using
Return lineItems
End Function
End Class
On my Form I would manually set up the DataGridView with the three relevant columns and I would have a private field in my Form class that would hold a
List(Of LineItem)
. You can populate this in the Forms Load event calling the
GetCurrentLineItems
method. Then you can either bind to the list of LineItems or manually enter each row (which I personally prefer) like this
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
currentLineItems = LineItem.GetCurrentLineItems()
colCode.DataSource = [Enum].GetValues(GetType(LineCode))
colCode.DataPropertyName = "LineCode"
For Each lineItem As LineItem In currentLineItems
Dim dgRow As New DataGridViewRow()
dgRow.CreateCells(DataGridView1)
dgRow.Cells(0).Value = lineItem.LineID
dgRow.Cells(1).Value = lineItem.LineCode
dgRow.Cells(2).Value = lineItem.LineDescription
DataGridView1.Rows.Add(dgRow)
Next
End Sub
where colCode is my ComboBox column
Hope this helps