Results 1 to 11 of 11

Thread: [RESOLVED] Databound Combobox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: Databound Combobox

    i need to filter :|

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    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?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: Databound Combobox

    cant find
    Please suggest corrections in my code.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Databound Combobox

    Quote Originally Posted by LuxCoder View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    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.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width