Setting the values that can be displayed in the child combo box
A tab has two combo boxes. The first one is the parent of the second one. The second one is the sub of the first one. It displays values for each item in the first combo box. That means each item in the first combo box has sub values in the second combo box.
How do I prepare the coding or set anything else (in the properties) or sth so that for each item in the parent combo box, ONLY the sub values of that parent is displayed in the second combo box.
The reason for this question is because for a parent item, the sub values of other parent items also appear in the second combo box.
Re: Setting the values that can be displayed in the child combo box
Can you show us how exactly you are done this. You may be missing a simple one line of code.
Re: Setting the values that can be displayed in the child combo box
To be more specific I am developing a system which has five tabs on 1 form (Tab 1 to Tab 5). Tab 1 is the main tab with one combo box which displays values which also will be displayed in the first combo box of Tab 2. Tab 1 also has a text box that is a description of the value in that combo box.
Tab 2 is the sub category of Tab 1. It has two combo boxes. The first combo box will display the value from Tab 1. The second combo box will display ONLY the sub values of each value from the first combo box(when selected). There is also a text box that will be disabled by default and only show the description of the second combo box value.
Tab 3 till Tab 5 do the exact same thing like Tab 2, and these are the tabs that do not work like it is explained above. (Tab 3 to Tab 5).
Below is the code for the beginning and also Tab 3. Is there anything wrong with it?
Code:
Imports System.Windows.Forms.DataGrid
Imports System.Data.SqlClient
Public Class InventoryMainForm
Private CatCodeDesc As String
Private CatCode As String
Private subcatcode As String
Private subcat1code As String
Private subcat2code As String
Private subcat3code As String
Private AddNew As Boolean
Dim DQcheck As New SqlCommand
Dim DHcheck As New SqlDataAdapter
Dim DScheck As New DataSet
Dim Dvcheck As New DataView
Private Sub InventoryMainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Me.CountryTableAdapter.Fill(Me.DsSales.country)
Me.Inv_itemsubcategoryTableAdapter.Fill(Me.dsInventory.inv_itemsubcategory)
Me.Inv_itemlocTableAdapter.Fill(Me.dsInventory.inv_itemloc)
Me.Inv_itemuomTableAdapter.Fill(Me.dsInventory.inv_itemuom)
Me.Inv_itemcategoryTableAdapter.Fill(Me.dsInventory.inv_itemcategory)
Me.Inv_itemCat3TableAdapter.Fill(Me.dsInventory.inv_itemCat3)
Me.Inv_itemCat4TableAdapter.Fill(Me.dsInventory.inv_itemCat4)
Me.Inv_itemCat5TableAdapter.Fill(Me.dsInventory.inv_itemCat5)
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text + " FrmLoad", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
'=========Inventory Item Sub Category 1 ===========
Private Sub subcat1BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles subcat1BindingNavigatorAddNewItem.Click
subcat1code = cmb1subcatcode.SelectedValue
cmb1subcatcode.Visible = False
txt1subcat.Visible = True
cmb1itemcat.Text = ""
txt1subcat.Text = ""
txt1subcatdesc.Text = ""
subcat1(False)
AddNew = True
cmb1itemcat.Focus()
End Sub
Private Sub subcat1(ByVal check As Boolean)
dgsubcategory1.Enabled = check
BindingNavigatorMoveFirstItem2.Enabled = check
BindingNavigatorMoveLastItem2.Enabled = check
BindingNavigatorMoveNextItem2.Enabled = check
BindingNavigatorMovePreviousItem2.Enabled = check
BindingNavigatorCountItem2.Enabled = check
BindingNavigatorPositionItem2.Enabled = check
subcat1BindingNavigatorRefreshItem.Visible = check
subcat1BindingNavigatorAddNewItem.Visible = check
subcat1BindingNavigatorEditItem.Visible = check
cmb1subcatcode.Visible = check
txt1itemcat.Visible = check
If check = False Then
check = True
subcat1BindingNavigatorDeleteItem.Text = "Cancel"
Else
check = False
subcat1BindingNavigatorDeleteItem.Text = "Delete"
End If
subcat1BindingNavigatorSaveItem.Visible = check
txt1subcat.Visible = check
cmb1itemcat.Visible = check
txt1subcatdesc.Enabled = check
End Sub
Private Sub subcat1BindingNavigatorEditItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles subcat1BindingNavigatorEditItem.Click
If dgsubcategory1.RowCount > 0 Then
subcat1code = cmb1subcatcode.SelectedValue
subcat1(False)
txt1subcat.Enabled = False
cmb1itemcat.Enabled = False
cmb1itemcat.Focus()
Else
MessageBox.Show("No record to edit.", Me.Text + " - Item Category Level 3", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub subcat1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles subcat1BindingNavigatorSaveItem.Click
Dim strmsg As String = ""
Try
If MessageBox.Show("Save Item?", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
If AddNew = True Then
If txt1subcat.Text.Trim = "" Then
strmsg = strmsg & "Required field - Category Code." & vbCrLf
Else
gSQLstr = "SELECT COUNT(ic3_code) FROM inv_itemCat3 WHERE ic3_code = " & cSQL(txt1subcat.Text) & ""
If gSQLcon.State = ConnectionState.Closed Then
gSQLcon.Open()
End If
DQcheck.Connection = gSQLcon
DQcheck.CommandText = gSQLstr
DHcheck.SelectCommand = DQcheck
DHcheck.Fill(DScheck, ("subcat1_list"))
Dvcheck.Table = DScheck.Tables("subcat1_list")
If Dvcheck.Table.Rows(0).Item(0) > 0 Then
strmsg = strmsg & "Required field - Category Code already exits, please assign another Category Code!" & vbCrLf
End If
DScheck.Clear()
gSQLcon.Close()
End If
End If
If cmb1itemcat.Text.Trim = "" Then
strmsg = strmsg & "Required field - Level 3 Category." & vbCrLf
End If
If txt1subcatdesc.Text.Trim = "" Then
strmsg = strmsg & "Required field - Description." & vbCrLf
End If
If strmsg = "" Then
If AddNew = True Then
dsInventory.inv_itemCat3.Addinv_itemCat3Row(Trim(txt1subcat.Text), txt1subcatdesc.Text, cmb1itemcat.Text)
Inv_itemCat3TableAdapter.Update(dsInventory.inv_itemCat3)
Inv_itemCat3TableAdapter.Fill(dsInventory.inv_itemCat3)
AddNew = False
Else
InvitemCat3BindingSource.EndEdit()
Inv_itemCat3TableAdapter.Update(dsInventory.inv_itemCat3)
End If
MessageBox.Show("Record saved", "Save", MessageBoxButtons.OK, MessageBoxIcon.None)
subcat1(True)
cmb1subcatcode.Enabled = True
cmb1subcatcode.SelectedValue = subcat1code
Else
MessageBox.Show(strmsg, Me.Text + " -Item Category Level 3", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
Catch ex As Exception
If ex.Message.Contains("Column 'ic3_code, ic3_itemcode' is constrained to be unique. ") = True Then
MessageBox.Show("The code already exits, please assign another code!", Me.Text + " - Item Category Level 3", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
Else
MessageBox.Show(ex.Message, Me.Text + " - Item Category Level 3(Save)", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Try
End Sub
Private Sub subcat1BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles subcat1BindingNavigatorDeleteItem.Click
Try
If dgsubcategory1.RowCount > 0 Then
If subcat1BindingNavigatorDeleteItem.Text = "Delete" Then
If MessageBox.Show("Are you sure to delete this record?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
gSQLstr = "SELECT COUNT(iim_subcat1) FROM inv_itemmaster WHERE iim_subcat1 = " & cSQL(cmb1subcatcode.Text) & ""
If gSQLcon.State = ConnectionState.Closed Then
gSQLcon.Open()
End If
DQcheck.Connection = gSQLcon
DQcheck.CommandText = gSQLstr
DHcheck.SelectCommand = DQcheck
DHcheck.Fill(DScheck, ("subcat1_list"))
Dvcheck.Table = DScheck.Tables("subcat1_list")
If Dvcheck.Table.Rows(0).Item(0) > 0 Then
MessageBox.Show("Record cannot be deleted!", Me.Text + " - Item Category Level 3", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
InvitemCat3BindingSource.RemoveCurrent()
Inv_itemCat3TableAdapter.Update(dsInventory)
MessageBox.Show("Record has been deleted.", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
DScheck.Clear()
gSQLcon.Close()
End If
Else
If AddNew = True Then
AddNew = False
End If
subcat1(True)
txt1subcatdesc.Enabled = False
refreshsubcat1()
cmb1subcatcode.SelectedValue = subcat1code
End If
Else
MessageBox.Show("No record to delete.", Me.Text + " - Item Category Level 3", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text + " - Item Category Level 3(Delete)", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
Private Sub subcat1BindingNavigatorRefreshItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles subcat1BindingNavigatorRefreshItem.Click
refreshsubcat1()
End Sub
Private Sub refreshsubcat1()
Try
Inv_itemCat3TableAdapter.Fill(dsInventory.inv_itemCat3)
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text + " - Item Category Level 3(Refresh)", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
Re: Setting the values that can be displayed in the child combo box
Going forward please use CODE tags when you post code.
Coming to your question, I do not see any code written for Combo box. What you need to do is this:
On Combox's SelectIndexChange event handler, you will need to either get the child values from the database or if you already have them in a dataset/datatable, you can use Select method on these to select only those values that are child of the item that has been selected in the parent combobox. Then bind the resulting datatable to the child combo box.
Re: Setting the values that can be displayed in the child combo box
Thank You so much for your feedback, I understand what you have answered, but could you provide me with an algorithm or coding so that I can better understand what you mean
Re: Setting the values that can be displayed in the child combo box
You will have to write your own code. Following are the steps:
1. Handle the Parent Combo Box's SelectedIndexChange Event.
2. In the event handler, get all the values from the database for the Child Combo Box using the selected item of the parent combo box as the criteria.
3. Bind / Load the values in to the child combo box.