[英]How to store data from comboBox2 and retrieve into comboBox2 by selecting a condition in comboBox1
Basically I have two categories: 基本上我有两类:
Category - A 类别-A
Category - B 类别-B
I will select category - A and generate number serials from 1 to 10. display in combobox 2 and save it. 我将选择类别-A,并生成从1到10的数字序列。显示在组合框2中并保存。
I will then select category B and generate number serials from 1 to 10, display in combobox 2 and save it. 然后,我将选择类别B并生成从1到10的数字序列,在组合框2中显示并保存。
when I close and open back the application, I want to see only those serials generated in category A in comboBox2. 当我关闭并重新打开该应用程序时,我只想查看comboBox2中类别A中生成的那些序列。
I have been trying to do it by adding a datagridview but it merges both category serials. 我一直在尝试通过添加datagridview来做到这一点,但它合并了两个类别序列。 Screenshot
屏幕截图
Category - A -- > Serials {1,2,3,4,5,6,7,8,9,10}
类别 -A->序列号
{1,2,3,4,5,6,7,8,9,10}
Category - B -- > Serials {11,12,13,14,15,16,17,18}
. 类别
{11,12,13,14,15,16,17,18}
>序列号{11,12,13,14,15,16,17,18}
。
so when I select Category A, I want to see only A serials in comboBox2 and nothing else. 因此,当我选择类别A时,我只想在comboBox2中看到A系列,而没有看到其他任何东西。
when I select Category B, I want to see only B serials in comboBox2 and nothing else. 当我选择类别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();
}
}
}
}
}
Here is a quick sample for you; 这是给您的快速样本;
I tried to set a form like you (not have textboxes to decide "to and from", I gave it myself directly) 我试图设置一个像您一样的表单(没有文本框来决定“往返”,我自己直接给了它)
Here is the trick, I defined it on global, 这是诀窍,是我在全局上定义的,
List<KeyValuePair<int, string>> vals = new List<KeyValuePair<int, string>>();
In FormLoad I add 2 string categories, 在FormLoad中,我添加了2个字符串类别,
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("CategoryA");
comboBox1.Items.Add("CategoryB");
}
And I have a button to generate serials, 我有一个按钮来生成序列号,
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");
}
}
And combobox's selectedindexchanged event, (Category's 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, 输出,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.