|
-
Oct 25th, 2009, 03:03 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Databound Combobox
On the form load event i want to populate my ComboBox1 to display unique items directly from the DB. For that i have set the DataSource property of the ComboBox1 to the selected field.
On the ComboBox1_SelectionChangeCommitted event i have set the filter for the DataSource which is working fine.
Now when i select an item from the ComboBox1 i get the filtered result in DataGridView but when i again drop down the combobox i see nothing other than the last selected item. Why is this happening? Is there a better way of populating ComboBox with the items directly from DB with default selected value as null?
Please help.
-
Oct 25th, 2009, 04:11 AM
#2
Re: Databound Combobox
You shouldn't be filtering the ComboBox itself when the user makes a selection, only the grid. That said, if you have two DataTables of related data then you can get the filtering to occur automatically with no code at all. Follow the CodeBank link in my signature and find my Master/Detail thread. You'll just bind the child BindingSource to the DataGridView instead of a second ComboBox but it's all the same apart from that.
-
Oct 25th, 2009, 12:35 PM
#3
Thread Starter
Hyperactive Member
-
Oct 25th, 2009, 06:04 PM
#4
Re: Databound Combobox
What you're saying doesn't make sense to me. Why would making a selection in a ComboBox prompt you to filter that same ComboBox? You're going to have to provide a proper, full and clear description of exactly what you're trying to achieve.
-
Oct 26th, 2009, 02:58 AM
#5
Thread Starter
Hyperactive Member
Re: Databound Combobox
I want to search data on DataGridView depending on selection of the ComboBox.
How do i add unique DBitems to a ComboBox? And how to refresh it, in case new item is added to the Field in DB?
-
Oct 26th, 2009, 03:47 AM
#6
Thread Starter
Hyperactive Member
Re: Databound Combobox
I am using the following code but i am getting only one item, when there are three in the db.
Code:
Dim myDBConnection As SqlClient.SqlConnection = Nothing
Dim myDBCommand As SqlClient.SqlCommand = Nothing
Dim myDBReader As SqlClient.SqlDataReader = Nothing
Try
myDBConnection = New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\ProjectMW\MachineWale\MachineWale\DatabaseMW.mdf;Integrated Security=True;User Instance=True")
myDBConnection.Open()
myDBCommand = New SqlClient.SqlCommand("SELECT Distinct Category FROM [Machine Profile]", myDBConnection)
myDBReader = myDBCommand.ExecuteReader(CommandBehavior.CloseConnection)
While myDBReader.HasRows
myDBReader.Read()
ComboBox2.Items.Add(myDBReader.Item("Category").ToString())
myDBReader.NextResult()
End While
Catch ex As Exception
MsgBox(" Error in Connection! ")
Exit Try
Exit Sub
Finally
'CLEAN UP DB OBJECTS
If myDBReader IsNot Nothing Then myDBReader.Close()
If myDBCommand IsNot Nothing Then myDBCommand.Dispose()
If myDBConnection IsNot Nothing Then myDBConnection.Close()
End Try
-
Oct 26th, 2009, 04:09 AM
#7
Re: Databound Combobox
You're reading the data incorrectly. Follow the Database FAQ link in my signature and from there you'll find more than one link that will show you how to use a DataReader.
-
Oct 26th, 2009, 04:24 AM
#8
Thread Starter
Hyperactive Member
Re: Databound Combobox
cant find 
Please suggest corrections in my code.
-
Oct 26th, 2009, 05:21 AM
#9
Re: Databound Combobox
 Originally Posted by LuxCoder
cant find 
If you "can't" find the information then you haven't looked. The second link in the VB.NET section of that FAQ is to a thread that I myself wrote and it contains the information you need. If you haven't found that it's because you have made no effort to find it.
-
Oct 26th, 2009, 06:27 AM
#10
Thread Starter
Hyperactive Member
Re: Databound Combobox
Sir, i cannot open any msdn document on my pc . So why wouldn't i not see or find the source you suggested?? I tried finding conditional recurring statement in your documentation but i could not find any.
-
Oct 26th, 2009, 07:03 AM
#11
Re: Databound Combobox
I didn't say anything about MSDN documentation. I didn't say anything about conditional recurring statements. I said follow the Database FAQ link in my signature, which leads to another thread her at VBForums, in the Database Development forum. In that thread there is a section dedicated to VB.NET and the second link in that section leads to a CodeBank thread of mine that contains numerous examples of using ADO.NET, including using a DataReader.
Certainly some people would just give you the solution here, but I'm not one of those. If I solve your problem then it means that you don't have to think, and I assume that anyone who wants to write software can think for themselves to a reasonable degree. I don't expect you to know everything, which is why I'm directing you to where you can find the information you need. What I do expect is that you can follow those directions when provided. Read what I posted, do what I said and you will solve your problem and quite likely learn more besides, which is exactly why I point people to information rather than providing a turn-key solution.
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
|