Click here to Skip to main content
15,888,027 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I am work on a Project For Production Management I have a Problem with ComboBoxs I need to filling a ComboBox by another ComboBox
And This is my code :
C#
public partial class FRM_ADD_PRODUCT : Form
   {
       BL.CLS_PRODUCTS prd = new BL.CLS_PRODUCTS();
       public FRM_ADD_PRODUCT()
       {
           InitializeComponent();
           CMBMAINCAT_NUM.DataSource = prd.GET_ALL_CATEGORIES();
           CMBMAINCAT_NAME.DataSource = prd.GET_ALL_CATEGORIES();
           CMBMAINCAT_NUM.DisplayMember = "CAT_NUM";
           CMBMAINCAT_NUM.ValueMember = "CAT_NAME";
           CMBMAINCAT_NAME.DisplayMember = "CAT_NAME";
           CMBMAINCAT_NUM.ValueMember = "CAT_NUM";
           CMBSUBCAT_NUM.DataSource = prd.GET_SUB_CATEGORIES();
           CMBSUBCAT_NAME.DataSource = prd.GET_SUB_CATEGORIES();
           CMBSUBCAT_NUM.DisplayMember = "SUB_CAT_NUM";
           CMBSUBCAT_NUM.ValueMember = "SUB_CAT_NAME";
           CMBSUBCAT_NUM.ValueMember = "CAT_NUM";
           CMBSUBCAT_NAME.DisplayMember = "SUB_CAT_NAME";
           CMBSUBCAT_NAME.ValueMember = "SUB_CAT_NUM";
           CMBSUBCAT_NAME.ValueMember = "CAT_NUM";

       }

and this is my code for DATAACCSESSLAYER.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace Management_Productions.DAL
{
    class DataAccessLayer
    {
        SqlConnection sqlconnection;

        //This Constructor Inisialize The connection object
        public DataAccessLayer()
        {
            sqlconnection = new SqlConnection(@"Server=.\KHALEDHOSNY; Database=Products_Management;Integrated Security=true");
        }

        //Method to open the connection
        public void open()
        {
            if (sqlconnection.State != ConnectionState.Open)
            {
                sqlconnection.Open();
            }
        }
        //method to close the connection
        public void Close()
        {
            if (sqlconnection.State == ConnectionState.Open)
            {
                sqlconnection.Close();
            }
        }

        //Method to Read Data from Database

        public DataTable SelectData(string stored_procedure, SqlParameter[] param)
        {
            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.CommandText = stored_procedure;
            sqlcmd.Connection = sqlconnection;

            if (param != null)
            {
                for (int i = 0; i < param.Length; i++)
                {
                    sqlcmd.Parameters.Add(param[i]);
                }
            }
            SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        //Method to Insert, Update, and Delete Data From Database
        public void Excutecommand(string stored_Procedure, SqlParameter[] param)
        {
            SqlCommand sqlcmd=new SqlCommand();
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.CommandText=stored_Procedure;
            sqlcmd.Connection = sqlconnection;


            if (param != null)
            {
                sqlcmd.Parameters.AddRange(param);
            }
            sqlcmd.ExecuteNonQuery();
        }
    }
}

and this is my code for CLS_PRODUCT.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace Management_Productions.BL
{
    class CLS_PRODUCTS
    {
        public DataTable GET_ALL_CATEGORIES()
        {
            DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
            DAL.open();
            DataTable Dt = new DataTable();
            Dt = DAL.SelectData("GET_ALL_CATEGORIES", null);
            DAL.Close();
            return Dt;
        }
        public DataTable GET_SUB_CATEGORIES()
        {
            DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
            DAL.open();
            DataTable Dt = new DataTable();
            Dt = DAL.SelectData("GET_SUB_CATEGORIES", null);
            DAL.Close();
            return Dt;
        }
    }
}


Please I need help for this and I want to know if there is any problems in my codes , please I want to Know how can I solve my problem , because I need to end my project fast .

this link a pic for my windows form for explain what i need i want to fill combo box number 1 by combo box number 2( http://www14.0zz0.com/2016/02/10/11/424961203.jpg ). combobox 2 items in a table in databsae and combobox 1 items in another datatable but there is a primary key between two tables .
and this link for my project source (Management_Productions[^]

What I have tried:

I am trying to use combobox.SelectedIndex to make what i need but i have failed.
sorry for my bad english :) :)
Posted
Updated 9-Feb-16 21:52pm
v5
Comments
dan!sh 5-Feb-16 5:04am    
What you are looking for is called "cascading combobox". You can find many samples to do so on web. Take a look at this question: cascading combobox in windows forms using c#[^]
[no name] 5-Feb-16 5:19am    
What is failed exactly? Secondly combobox.SelectedIndex is not there in code snippet.
KhaledHosny 5-Feb-16 16:02pm    
yes secondly combobox.selectedindex
BillWoodruff 5-Feb-16 18:55pm    
To assist you with this, I'd need to see your code that actually handles the selection in the primary ComboBox.
KhaledHosny 6-Feb-16 4:07am    
I dont know the right code for do this i ask for help for that

a. you initialize the first ComboBox from the results of a database query.

a.1. set the 'DataSource of the ComboBox to the query result (transforming the query result into a set of 'objects as required),

a.2. then set the 'ValueMember and 'DisplayMember properties of the ComboBox as needed.

a.3. implement a 'SelectionIndexChanged EventHandler for the ComboBox

b. based on the user's actions ... making a selection in the first ComboBox ...

b.1. you make another query to the database to retrieve the appropriate values for the second ComboBox, and you set its values using the techniques mentioned in a.1.

b.2. implement an event handler as in a.3.

A lot depends here on what the structure of your database is (many tables ?), and whether the database contents (rows, fields) are small enough that you can do one (or a series of) query and get all the data you'll need in the UI, and then create a Collection to hold the sets of values for the second ComboBox ... rather than having to load the second ComboBox values every time there is a selection in the first ComboBox.

A rough sketch:
C#
private int currentSelectedIndex1 = 0;
private int currentSelectedIndex2 = 0;

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // ignore if no selection
    if (comboBox1.SelectedIndex < 0) return;

    currentSelectedIndex1 = comboBox1.SelectedIndex;

    // get the values to go in the second ComboBox here
    // by database query ?
    // by binding to a database table ?
    // by using a "pre-compiled" selection from sets of "data" ?

    // fill the second ComboBox with appropriate values
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    // ignore if no selection
    if (comboBox2.SelectedIndex < 0) return;

    currentSelectedIndex2 = comboBox2.SelectedIndex;

    // you have a user choice: do something with it
}
 
Share this answer
 
v2
I must be missing something... this seems too simple:

From the UI thread:
C#
foreach (Object o in comboBox1.Items)
{
    comboBox2.Items.Add(o);
}


Or, if you're need to work with the combo boxes from a background thread:
C#
private void BackgroundThread()
{
    this.Invoke((MethodInvoker)delegate()
    {
        foreach (Object o in comboBox1.Items)
        {
            comboBox2.Items.Add(o);
        }
    });
}


- Pete
 
Share this answer
 
v6
Comments
KhaledHosny 6-Feb-16 13:52pm    
I am already had combobox item in sql databas

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900