dcsimg
Results 1 to 9 of 9

Thread: Filling Combobox disables form

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    56

    Filling Combobox disables form

    Hi, not sure what I'm doing wrong here, I am trying to create an add stock form to my table, this table will reference the items table to pick the stock that I am updating.

    there are two tables

    Code:
    CREATE TABLE [dbo].[TblItems] (
        [ItemID]   INT          IDENTITY (1, 1) NOT NULL,
        [ItemCat]  VARCHAR (15) NOT NULL,
        [ItemName] VARCHAR (30) NOT NULL,
        [ItemUnit] VARCHAR (5)  NOT NULL,
        PRIMARY KEY CLUSTERED ([ItemID] ASC)
    and
    Code:
    CREATE TABLE [dbo].[TblStock] (
        [StockID]  INT        NOT NULL,
        [ItemID]   INT        NOT NULL,
        [ItemQTY]  FLOAT (53) NOT NULL,
        [ItemCost] MONEY      NOT NULL,
        [UnitCost] MONEY      NOT NULL,
        PRIMARY KEY CLUSTERED ([StockID] ASC),
        CONSTRAINT [FK_TblStock_TblItems] FOREIGN KEY ([ItemID]) REFERENCES [dbo].[TblItems] ([ItemID])
    );
    On the form itself, I have the datagrid and details components for TblStock.

    For the [ItemID] field I have a combobox [ItemIDComboBox]

    Using the designer I checked the box to "Use Data Bound Items"
    And set the following values...

    Datasource = TblItemsBindingSource
    DisplayMember = ItemName
    ValueMember = ItemID
    Selected Value = ??? (Not sure if this should be TblItemsBindingSource:ItemID or TblStockBindingSource:ItemID)

    The combobox itself works and populates the dropdownlist with items from TblItems

    BUT

    when I select an item from the list, the rest of the form is disabled, I cannot click into or type into any other fields and Save seems to be disabled.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,451

    Re: Filling Combobox disables form

    You don't set the SelectedValue. The DataSource provides the items, the DisplayMember specifies what property/column of those items will be displayed in the control and the ValueMember specifies what property/column of those items will be exposed via the SelectedValue. When the user selects an ItemName value in the drop-down list, the corresponding ItemID value will be exposed via the SelectedValue. That's how you get the key for the selected item. If you assign an ItemID value to SelectedItem, the corresponding ItemName will be displayed.

    As for your issue, there's nothing about selecting an item in a ComboBox that should disable anything. If controls are actually being disabled, short of corruption, you'd have to be doing it. The obvious explanation is that you have code that goes into an infinite loop and that freezes the UI. Are you handling any events of that ComboBox?

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    56

    Re: Filling Combobox disables form

    Not that I know of...

    I picked a selected valu because I couldn't seem to close the pop up for data binding if I didn't. Could that be causing the loop?

    I haven't added any additional code, I have done everything through the designer.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,451

    Re: Filling Combobox disables form

    Quote Originally Posted by kragen2179 View Post
    I picked a selected valu because I couldn't seem to close the pop up for data binding if I didn't. Could that be causing the loop?
    I've never set the SelectedValue in that context. I would think that doing so would select a specific item when the control is first populated. Unless you want a specific item selected, there doesn't seem to be a reason to set it. I don't see how that could cause an issue on a later selection but maybe. Is the value you're setting it to valid?

    When the issue shows itself, does the form repaint properly if you drag another window over it? Can you drag the form?

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    56

    Re: Filling Combobox disables form

    Ok tried again...

    I have 2 forms frmitems which fills the items table, and frmstock to fill the stock table.

    I dragged both the datagrid and details for tblitems on to the frmitems ran it and it works fine, can fill the table no issues.

    Into frmstock

    Dragged the datagrid on to the form, set the ItemID value to combobox and dragged the details onto the form.

    Clicked the tiny arrow on the combo box, selected use data bound items and set bindingsource to tblitems bindingsource, set displaymember to ItemName, and set valuemember to ItemID.

    (I want to display the item name, but the foreign key is the item I'd so it needs to select that.)

    When I run the program, I open the items form and add a couple of items. Still all good.

    Goto frmstock to add stock, and the items show in the drop down list, but when I save the record, the stock I'd auto increments but the datagrid doesn't fill.

    In addition all the other text boxes are fine until I select the combox, at which point I cannot click out of the combobox, and the close button stops working.

  6. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,123

    Re: Filling Combobox disables form

    I wonder if using the Combobox is a good way to go.
    I think that I would use a Treeview with the Products sorted by Category
    then when 'Click" on a Product use that ID to Load whatever Table with Detail Information you need

    here a sample to Load a Treeview.... Category -> to Products and get the ProductID
    to Load a further Table to a Listview or Datagridview (Load the Listview or DGV is for you todo)

    Code:
    Option Strict On
    
    Imports System.Data.OleDb
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Load_tree()
        End Sub
    
        Private Sub Load_tree()
            Dim myConnString As String = "Provider=Microsoft.JET.OlEDB.4.0;data source=E:\Northwind.mdb"
            Dim daMyName As New OleDbDataAdapter
            Dim dsMyName As New DataSet
            Dim recCount As Integer
    
            Dim mySelectQuery As String = "SELECT DISTINCT Categories.CategoryID, " & _
            "Categories.CategoryName, Products.ProductName,  Products.ProductID " & _
             "FROM Categories INNER JOIN Products ON " & _
             "Categories.CategoryID = Products.CategoryID;"
    
            Dim myConnection As New OleDbConnection(myConnString)
            Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)
            myConnection.Open()
    
            daMyName.SelectCommand = myCommand
            daMyName.Fill(dsMyName)
    
            If Not dsMyName.Tables(0).Rows.Count > 0 Then
                MessageBox.Show("There were no results found.", "No Results Found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    
            Else
                'check in the Database first how many childnodes should appear,
                'in my case 77, which is shown correctly from Rows.Count
                Dim s As String = "TOTAL NUMBER OF RECORDS ARE : "
                MessageBox.Show(s & dsMyName.Tables(0).Rows.Count, "Found..", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
                recCount = dsMyName.Tables(0).Rows.Count
                'Clear any nodes so we don't load a tree into a tree
                TreeView1.Nodes.Clear()
    
                Dim parentRow As DataRow
                Dim tbl As New DataTable
                Dim rowNdx As Integer
                rowNdx = 0
                daMyName.Fill(tbl)
                Dim strName As String
    
                For Each parentRow In tbl.Rows
                    If rowNdx = recCount Then
                        Exit For
                    End If
                    strName = dsMyName.Tables(0).Rows(rowNdx)("CategoryName").ToString() 'set the next name
                    Dim pnode As TreeNode
                    'pnode is the Parent
                    pnode = TreeView1.Nodes.Add(dsMyName.Tables(0).Rows(rowNdx)("CategoryName").ToString())
    
                    Dim cnode As TreeNode
                    'cnode is the Child 
                    cnode = New TreeNode
                    Do While dsMyName.Tables(0).Rows(rowNdx)("CategoryName").ToString() = strName And rowNdx < recCount
                        cnode = pnode.Nodes.Add(dsMyName.Tables(0).Rows(rowNdx)("ProductName").ToString())
    
                        'ProductID in Tag
                        cnode.Tag = dsMyName.Tables(0).Rows(rowNdx)("ProductID").ToString()
    
                        rowNdx = rowNdx + 1
                        If rowNdx = recCount Then
                            Exit Do
                        End If
                    Loop
                Next parentRow
                myConnection.Close()
            End If
        End Sub
    
        Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
            'a click on the Productname in Treeview will show the ProductID in MessageBox 
            Dim s As String = TreeView1.SelectedNode.Text
            Dim s1 As String = CStr(TreeView1.SelectedNode.Tag)
            MessageBox.Show("Product     : " & s & vbNewLine & "Procuct ID : " & s1)
        End Sub
    and a Image of what is look like
    Name:  TreeLoad.jpg
Views: 33
Size:  38.8 KB


    HTH
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    56

    Re: Filling Combobox disables form

    Thank you for the suggestion, but that seems rather cumbersome for what I'm looking for.

    The reason for the combo box, is so that the user can select the item for which they are adding stock.

  8. #8
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,123

    Re: Filling Combobox disables form

    Quote Originally Posted by kragen2179 View Post
    Thank you for the suggestion, but that seems rather cumbersome for what I'm looking for.

    The reason for the combo box, is so that the user can select the item for which they are adding stock.
    cumbersome? not I don't think so
    you can have 1 DGV to Execute muliple SQL Statemants .. Read Stock, Read Stock Transaction (Date Between) etc...
    it's all about the Products with Product ID, which the Treeview can display in a nice way

    but that's up to you
    Last edited by ChrisE; Nov 20th, 2019 at 09:46 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    56

    Re: Filling Combobox disables form

    I thi k we may be misunderstanding each other. I meant that the tree view would be cumbersome on my form

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width