如何通过在comboBox1中选择条件来存储来自comboBox2的数据并将其检索到comboBox2中

时间:2016-08-03 13:46:25

标签: c# datagridview

基本上我有两个类别:

类别 - A
类别 - B

我将选择类别 - A并生成从1到10的数字序列。 在组合框2中显示并保存。

然后,我将选择B类并生成1到10的数字序列,在组合框2中显示并保存。

当我关闭并打开应用程序时,我想只看到comboBox2中类别A中生成的那些序列号。

我一直试图通过添加datagridview来实现它,但它合并了两个类别序列。 Screenshot

类别 - A - >连续{1,2,3,4,5,6,7,8,9,10}
类别 - B - >连续{11,12,13,14,15,16,17,18}

所以当我选择A类时,我想只看到comboBox2中的A连续剧而没有别的。

当我选择B类时,我想在comboBox2中只看到B系列,而不是其他。

private void GenSerialBookButton_Click(object sender, EventArgs e)
    {

        if (comboBox1.SelectedIndex == 0)
        {

            from = int.Parse(textBox2.Text);
            to = int.Parse(textBox3.Text);

            result = to - from;  

            for (int i = 0; i <= result; i++)
            {
                comboBox2.Items.Add(from + i);

                this.SerialBookDataBaseBindingSource.AddNew();
                dataGridView1.Rows[i].Cells[1].Value = from + i;

                }
            MessageBox.Show("Serial Book Generated Success", "Success");
            }

        if (comboBox1.SelectedIndex == 1)
        {

            from = int.Parse(textBox2.Text);
            to = int.Parse(textBox3.Text);

            result = to - from;

            for (int i = 0; i <= result; i++)
            {

                comboBox2.Items.Add(from + i);
                this.SerialBookDataBaseBindingSource.AddNew();

                dataGridView1.Rows[i].Cells[1].Value = from + i;
            }

            MessageBox.Show("Serial Book Generated Success", "Success");

        }


    }


    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    // Clear previous list
    a: if (comboBox2.Items.Count > 0)
        {
            comboBox2.Items.RemoveAt(0);
            goto a;
        }


        if (comboBox1.SelectedIndex == 0)
        {

            comboBox2.Items.Clear();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    comboBox2.Items.Add(row.Cells[1].Value);
                    MessageBox.Show("Adding: " + (row.Cells[1].Value.ToString()));
                    comboBox2.Refresh();
                }

            }

            if (comboBox1.SelectedIndex == 1)
            {

                comboBox2.Items.Clear();
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        comboBox2.Items.Add(row.Cells[1].Value);
                        MessageBox.Show("Adding: " + (row.Cells[1].Value.ToString()));
                        comboBox2.Refresh();
                    }

                }

            }


        }
    }

1 个答案:

答案 0 :(得分:2)

以下是您的快速示例;

我尝试设置一个像你一样的表格(没有文字框来决定“往返”,我自己直接给了它)

这是诀窍,我在全球定义了它,

List<KeyValuePair<int, string>> vals = new List<KeyValuePair<int, string>>();

在FormLoad中,我添加了两个字符串类别

 private void Form1_Load(object sender, EventArgs e)
    {
        comboBox1.Items.Add("CategoryA");
        comboBox1.Items.Add("CategoryB");
    }

我有一个生成连续剧的按钮,

private void button1_Click(object sender, EventArgs e)
{
      if (comboBox1.SelectedIndex == 0)
            {


                for (int i = 0; i <= 10; i++)
                {
                    string item = i + "A"; // Given"A" to seperate from each other
                    comboBox2.Items.Add(item);
                    vals.Add(new KeyValuePair<int, string>(0, item)); // CatA has 0 key value

                }

                MessageBox.Show("Serial Book Generated Success", "Success");
            }

            if (comboBox1.SelectedIndex == 1)
            {


                for (int i = 0; i <= 5; i++)
                {
                    string item = i + "B"; // Given "B" to seperate from each other
                    comboBox2.Items.Add(item);
                    vals.Add(new KeyValuePair<int, string>(1, item)); // CatB has 1 key value
                }

                MessageBox.Show("Serial Book Generated Success", "Success");

            }
 }

和combobox的selectedindexchanged事件,(类别的组合框)

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {

                comboBox2.Items.Clear();
                foreach (var item in vals)
                {
                    if (item.Key == 0) //If Key value is 0, if it is CategoryA
                    {
                        comboBox2.Items.Add(item.Value);
                       // MessageBox.Show("Adding: " + (item.Value.ToString()));
                        comboBox2.Refresh();
                    }
                }

            }

            if (comboBox1.SelectedIndex == 1)
            {

                comboBox2.Items.Clear();
                foreach (var item in vals)
                {
                    if (item.Key == 1)    //If Key value is 1, if it is CategoryB
                    {

                        comboBox2.Items.Add(item.Value);
                        //MessageBox.Show("Adding: " + (item.Value.ToString()));
                        comboBox2.Refresh();
                    }
                }
            }


        }

<强>输出

outputs 希望有帮助,