-
Jun 1st, 2020, 10:58 AM
#1
Thread Starter
New Member
[RESOLVED] How to show data on Drop Down List type combo box from a database?
I have the following requirement.
- I have a combo box which i want to fill with data from a database.
- Combo box type is - DropDownList. (This is required so the user can not edit data.)
- Can save the data from combobox to table using combobox.text property. But while it is a DropDownList type, its impossible to show data extracted from the table even though the values are identical.
- Some one proposed me to use the select index option. But i couldn't figure out how.
I already tried with the following procedure. But its not showing data in DropDownList combo boxes. (I have my own class which i have used below)
Private Sub filldisProfiles()
Dim sqlString As String = "SELECT discount_profile FROM tb_discount_profiles"
Dim tempTb As DataTable
Dim myTbClass As myClassTableActivities = New myClassTableActivities()
tempTb = myTbClass.myFunctionFetchTbData(sqlString)
cmbDisProfile.DataSource = tempTb
cmbDisProfile.DisplayMember = "discount_profile"
End Sub
I also tried with the following method. But index value is always -1.
cmbDisProfile.SelectedIndex = cmbDisProfile.FindStringExact(trim(tempTb.Rows(0).Item("it_discount_profile")))
What am i doing wrong? Is there any other better way?. Any advice will be highly appreciated.
-
Jun 1st, 2020, 03:28 PM
#2
Re: How to show data on Drop Down List type combo box from a database?
You're missing the ValueMember property....
Code:
cmbDisProfile.DisplayMember = "discount_profile"
cmbDisProfile.ValueMember = "discount_profile"
cmbDisProfile.DataSource = tempTb
Now when you bind a datatable to the combobox, it will automatically select the right item (presuming that the values actually match).
-tg
edit - also, don't set the selected Index... just set the .Value ...
-tg
-
Jun 1st, 2020, 05:40 PM
#3
Re: How to show data on Drop Down List type combo box from a database?
Originally Posted by techgnome
also, don't set the selected Index... just set the .Value ...
That should be SelectedValue.
-
Jun 1st, 2020, 10:15 PM
#4
Thread Starter
New Member
Re: How to show data on Drop Down List type combo box from a database?
Originally Posted by techgnome
You're missing the ValueMember property....
Code:
cmbDisProfile.DisplayMember = "discount_profile"
cmbDisProfile.ValueMember = "discount_profile"
cmbDisProfile.DataSource = tempTb
Now when you bind a datatable to the combobox, it will automatically select the right item (presuming that the values actually match).
-tg
edit - also, don't set the selected Index... just set the .Value ...
-tg
Thanks
-
Jun 1st, 2020, 10:16 PM
#5
Thread Starter
New Member
Re: How to show data on Drop Down List type combo box from a database?
-
Jun 1st, 2020, 10:19 PM
#6
Thread Starter
New Member
Re: How to show data on Drop Down List type combo box from a database?
-
Jun 4th, 2020, 11:11 PM
#7
Lively Member
Re: How to show data on Drop Down List type combo box from a database?
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim colors = New Dictionary(Of String, String)()
colors("10") = "Red"
colors("20") = "Blue"
colors("30") = "Green"
colors("40") = "Yellow"
ComboBox1.DataSource = New BindingSource(colors, Nothing)
ComboBox1.DisplayMember = "Value"
ComboBox1.ValueMember = "Key"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(Convert.ToString(ComboBox1.Text + " " + ComboBox1.SelectedValue))
End Sub
End Class
-
Jun 5th, 2020, 10:03 PM
#8
Re: How to show data on Drop Down List type combo box from a database?
Originally Posted by Prahlad
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim colors = New Dictionary(Of String, String)()
colors("10") = "Red"
colors("20") = "Blue"
colors("30") = "Green"
colors("40") = "Yellow"
ComboBox1.DataSource = New BindingSource(colors, Nothing)
ComboBox1.DisplayMember = "Value"
ComboBox1.ValueMember = "Key"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(Convert.ToString(ComboBox1.Text + " " + ComboBox1.SelectedValue))
End Sub
End Class
While it isn't supposed to make a difference, sometimes it does ... set the .DataSoure last after setting the .DisplayMember and .ValueMember ... just make it a habit. Setting the .DataSource first can sometimes, sometimes, lead to odd display issues.
-tg
-
Jun 6th, 2020, 09:28 AM
#9
Thread Starter
New Member
Re: How to show data on Drop Down List type combo box from a database?
Originally Posted by techgnome
While it isn't supposed to make a difference, sometimes it does ... set the .DataSoure last after setting the .DisplayMember and .ValueMember ... just make it a habit. Setting the .DataSource first can sometimes, sometimes, lead to odd display issues.
-tg
Thank you
-
Jun 6th, 2020, 09:29 AM
#10
Thread Starter
New Member
Re: How to show data on Drop Down List type combo box from a database?
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
|