[RESOLVED] Dataitem class
Guys,
I have used this class in VB.NET code to create dataitems that I can add to comboboxes, listboxes etc. I can display the text item, and still get hold of other database fields associated with that row by using . .
Ctype(cb_names.selecteditem,dataitem).Altfield1 for example.
Can I do something like this in ASP.NET ? I have tried using this but I'm getting an overload resolution fail when adding items to a listbox.
Basically, I need to add items to a list box from a datatable, I need to display the text field, but retain the id field so I know which record the users are selecting.
Am I going about this the right way ?
Cheers
Bob
VB Code:
Public Class DataItem
Public id As Integer
Public text As String
Public row As DataRow
Public Overrides Function ToString() As String
Return text
End Function
Public Sub New(ByVal row As DataRow)
Me.new(row, "id", "name")
End Sub
Public Sub New(ByVal id As Integer, ByVal text As String)
Me.id = id
Me.text = text
End Sub
Public Sub New(ByVal row As DataRow, ByVal id As Integer, ByVal text As String)
Me.row = row
Me.id = id
Me.text = text
End Sub
Public Sub New(ByVal row As DataRow, ByVal idfield As String, ByVal textfield As String)
id = Nz(row(idfield), 0)
text = Nz(row(textfield), "")
Me.row = row
End Sub
This is an example of how I've used it in VB.NET . . .
VB Code:
cb_rfiissuedby.Items.Clear()
dt = db.ContractStaff.Table(CurrentContract.ID)
For Each row As DataRow In dt.Rows
cb_rfiissuedby.Items.Add(New DataItem2(row, "ID", "Name", "CompanyName"))
Next
dt.Dispose()
dt = Nothing