|
-
Sep 19th, 2002, 10:06 AM
#1
Thread Starter
Member
Combo Box Challenge Does Anyone know the answer
Has anyone got a good example of loading a data reader into a combo box. I don't want to to just load the data but also have the Id field in there as well. I can do this with a dataset, but the perfomance is very slow. I can't find any examples of doing this a datareader. Also in Vb6 you could also make performance even faster by outputing the recordset into a .dat file and loading it into the combo box from there. Has anyone got any good examples of this. I looked extensively on both these subjects and can find nothing. Help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-
Sep 20th, 2002, 10:59 AM
#2
Thread Starter
Member
Surely someone must know this. If I had posted a thread entitled VB.net is crap I going back to VB6 I would of had 20 replies By now..........
-
Sep 20th, 2002, 11:59 AM
#3
There are a few different ways of doing this, but mainly the same method as the dataset should work with the datareader. You can also make your own datarow class and inherit the other one to override the tostring method and add that. You could make a custom class with the info you want and add that to the combo. If you switch around the method you use for the dataset it should look something like this:
VB Code:
Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader()
Combo1.Items.Clear()
Do While dr.Read()
Combo1.Items.Add(New PtDiagnosisItem(dr.Item("PtDxID"), dr.Item("ICD9"), dr.Item("Description")))
Loop
dr.Close()
dr = Nothing
cmd.Dispose()
-
Sep 26th, 2002, 08:50 AM
#4
Thread Starter
Member
Sorry I did not make myself clear on this. I want the id to be hidden but the description to be shown, after creating a dataset I would use the following code to do this
With CmbEquipment
.DataSource = MydataSet
.DisplayMember = "Equipment"
.ValueMember = "EquipmentId"
End With
How do I get the same but with a datareader?
-
Sep 26th, 2002, 10:05 AM
#5
Do you mean you want to bind a control to a datareader?
You can't the datareader is a forward only, read only reccordset design. You can fill it from the datareader but you can't bind to it, besides databind sucks for reasons like this.
-
Sep 26th, 2002, 10:25 AM
#6
Thread Starter
Member
the combo box will be used to update a table through an update statement but the table holds the value not the description. So I want to update the value back to the database. The user though will need to see only the description as the Id is meaningless to him
-
Sep 26th, 2002, 10:38 AM
#7
So why is it that you want to use a datareader? You can't update with a datareader. In fact you can't even move backwards in a datareader. You should be able to use a dataset just fine.
-
Sep 27th, 2002, 05:13 AM
#8
Thread Starter
Member
the issue as I mentioned is speed the dataset is slower than a datareader. The form as such is then painfully slow to run at runtime. I just want to be able to populate the combo box with a description and hide the ID.
You could do this in VB6 with a forwardly only cursor then update the database with an update sql statement. Is it seriously the case that this cannot be done in VB.net. if it is, then VB.net is a big step backwards.
-
Sep 27th, 2002, 10:23 AM
#9
You can populate with a datareader but you can't use the datasource property to do it, that is just for databinding.
Actually I guess you canuse the datasource property for more than just standard databinding, you can even bind to classes. So with that then I'm not sure if you can bind directly to the datareader, but if not then you can fill a genericlist class and use it.
VB Code:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'make data to fill list
Dim MyList As New ArrayList()
MyList.Add(New GenericList("Jack Hammer", 1))
MyList.Add(New GenericList("Sledge Hammer", 2))
MyList.Add(New GenericList("Screwdriver", 3))
MyList.Add(New GenericList("Wrench", 4))
'bind list
ComboBox1.DataSource = MyList
ComboBox1.DisplayMember = "Display"
ComboBox1.ValueMember = "ID"
End Sub
End Class
Public Class GenericList
Private _Display As String
Private _ID As Integer
Public Property Display() As String
Get
Return _Display
End Get
Set(ByVal Value As String)
_Display = Value
End Set
End Property
Public Property ID() As Integer
Get
Return _ID
End Get
Set(ByVal Value As Integer)
_ID = Value
End Set
End Property
Public Sub New()
MyBase.new()
End Sub
Public Sub New(ByVal display As String, ByVal id As Integer)
Me.Display = display
Me.ID = id
End Sub
End Class
Or to fill the class from the datareader:
VB Code:
Dim dr As New Data.SqlClient.SqlDataReader(cmd)
Dim MyList As New ArrayList()
Do While dr.read
MyList.Add(New GenericList(dr.Item("Equipment"), dr.Item("EquipmentID"))
Loop
'bind list
ComboBox1.DataSource = MyList
ComboBox1.DisplayMember = "Display"
ComboBox1.ValueMember = "ID"
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|