如何从同一表单中的另一个组合框填充组合框?

时间:2013-12-12 15:54:08

标签: c# winforms combobox

创建了两个comboBox和一个名为MAINCATE的表。

我有一个代码,但坚持确定我应该用什么SQLQuery来填充第二个组合框,由第一个组合框决定。

我只需要一些帮助就如何根据第一个组合框拾取的mainCate填充第二个组合框。

我需要做一些像..如果组合框1 mainCate是“食物”,那么组合框2应该显示“生,熟,水果和蔬菜”

这是MAINCATE表中的内容 - (http://i.imgur.com/qR90Z2B.png

这是我的代码: -

DataSet ds1;
DataSet ds2;

public User()
{
    InitializeComponent();
}

private void User_Load(object sender, EventArgs e)
{

    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
    conn.Open();

    SqlDataAdapter daMain = new SqlDataAdapter("SELECT * FROM MAINCATE", conn);
    ds1 = new DataSet();
    daMain.Fill(ds1, "Maincate");
    mainCatU.DisplayMember = "mainCate";
    mainCatU.ValueMember = "mainCate";
    mainCatU.DataSource = ds1.Tables["MAINCATE"];
    mainCatU.DropDownStyle = ComboBoxStyle.DropDownList;
    mainCatU.Enabled = true;

    SqlDataAdapter daSub = new SqlDataAdapter("SELECT >What should i do here?<", conn);
    ds2 = new DataSet();
    daSub.Fill(ds2, "Subcate");
    subCatU.DisplayMember = "Subcat1";
    subCatU.ValueMember = "Subcat";
    subCatU.DataSource = ds2.Tables["MAINCATE"];
    subCatU.DropDownStyle = ComboBoxStyle.DropDownList;
    subCatU.Enabled = true;
    conn.Close();
}

private void mainCatU_SelectionChangeCommitted(object sender, EventArgs e)
{
    //have no idea if a code should be here..
}

或者我应该这样做吗?

SqlCommand cmd = new SqlCommand("select Subcat1,Subcat2,Subcat3,Subcat4 from MAINCATE where mainCate=@mainCate;", con);

=========================================

@philip - 把它放在页面加载上面重复我的代码 - 它没有工作..

string result = mainCatU.SelectedItem.ToString();

            SqlDataAdapter daSub = new SqlDataAdapter("SELECT * FROM MAINCATE where mainCate = " + result , conn);
            ds2 = new DataSet();
            daSub.Fill(ds2, "Subcate");
            subCatU.DisplayMember = "Subcat1";
            subCatU.ValueMember = "Subcat1";
            subCatU.DataSource = ds1.Tables["MAINCATE"];
            subCatU.DropDownStyle = ComboBoxStyle.DropDownList;
            subCatU.Enabled = true;

甚至尝试过

SqlDataAdapter daSub = new SqlDataAdapter("SELECT * FROM MAINCATE where mainCate=@result", conn);

3 个答案:

答案 0 :(得分:1)

我认为这就是你要找的东西:

使用当前代码填充第一个组合框。

然后要填充第二个组合框,您需要连接第一个组合框selectionchangemit。但是为什么不只是使用标准事件?

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Here use if statements to capture what value is set
        if (comboBox1.SelectedIndex = 1)
        //If selected value is Vehicles
        {
            //Then SELECT * FROM MainCate WHERE MainCate = 'Vehicles'
            //This is possibly incorrect as I don't know how your DBTable is structured

            //Same code as before
            //Set this data to the second combobox
        }
    }

行?所以看看实现这个,如果你想重构这个,你可以,而不是使用你可以参数化的IF语句 -

 string result = comboBox1.SelectedItem.ToString();

 SELECT * FROM MainCate WHERE MainCate = result

显然这不会编译所以不要复制然后粘贴它,然后回来说它不起作用。它需要像以前一样实现,但不是每次都对结果进行硬编码,而是使用参数。

就个人而言,我不会在一个班级中拥有这一切,但你可能更喜欢这种方式。

答案 1 :(得分:1)

实际上,您不需要另外的SQL查询,因为您已经从数据库中获取了所有maincate记录。您只需使用字典来存储此记录。

首先定义Dictionary<string,List<string>>

在这里定义(!)

 DataSet ds1;
 DataSet ds2;
 Dictionary<string,List<string>> allRecords = new Dictionary<string,List<string>>();

然后:(我编辑你的代码)

SqlDataAdapter daMain = new SqlDataAdapter("SELECT * FROM MAINCATE", conn);
ds1 = new DataSet();
daMain.Fill(ds1, "Maincate");

DataTable dt = ds1.Tables["MAINCATE"];
foreach (DataRow dr in dt.Rows)
{
   List<string> SubCats = new List<string> {
     dr["Subcat1"].ToString(), 
     dr["Subcat2"].ToString(),
     dr["Subcat3"].ToString(),
     dr["Subcat4"].ToString()
   };
 allRecords.Add(dr["mainCate"].ToString(),SubCats);
 mainCatU.Items.Add(dr["mainCate"].ToString());
}

mainCatU.DropDownStyle = ComboBoxStyle.DropDownList;
mainCatU.Enabled = true;

然后你需要像这样处理mainCatU selectionchanged:

if(allRecords.ContainsKey(mainCatU.SelectedItem.ToString())) {
    subCatU.DataSource = allRecords[mainCatU.SelectedItem.ToString()];
}

答案 2 :(得分:-1)

ComboBox1,ComboBox2 - 您只想使用ComboBox1填充ComboBox2选择更改。所以,首先绑定你的ComboBox1。然后为ComboBox1创建一个事件:

 private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
                {
                    string value1 = ComboBox1 .SelectedValue.ToString();
                    LoadComboBox2 ();        
                }

获取您的ComboBox1选定值并使用它来填充ComboBox2。

private void LoadComboBox2 ()
        {
            DataRow dr;

            SqlConnection con = new SqlConnection(@"Data Source=name;Initial Catalog=dbName;User ID=sa;Password=sa123");
            con.Open();
            SqlCommand cmd = new SqlCommand("select id,name from table where id=@ID", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);

            dr = dt.NewRow();
            dr.ItemArray = new object[] { 0, "--Select--" };
            dt.Rows.InsertAt(dr, 0);

            ComboBox2 .ValueMember = "ID";

            ComboBox2 .DisplayMember = "Name";
            ComboBox2 .DataSource = dt;
            con.Close();

        }