0

Basically I have two categories:

Category - A
Category - B

I will select category - A and generate number serials from 1 to 10. display in combobox 2 and save it.

I will then select category B and generate number serials from 1 to 10, display in combobox 2 and save it.

when I close and open back the application, I want to see only those serials generated in category A in comboBox2.

I have been trying to do it by adding a datagridview but it merges both category serials. Screenshot

Category - A -- > Serials{1,2,3,4,5,6,7,8,9,10}
Category - B -- > Serials{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.

when I select Category B, I want to see only B serials in comboBox2 and nothing else.

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();
                    }

                }

            }


        }
    }
Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37
Patrick
  • 217
  • 1
  • 5
  • 19
  • So, if you want to close application and open it again, there won't be any data. You need to use a source to store data. (DBMS, TextFile, XML or something else.) – Berkay Yaylacı Aug 03 '16 at 13:50
  • when I change index from comboBox 1, I'm repopulating stored data when the application is running. I'm not saving data in my code currently, I can do that, but that is not the problem, problem is that I want to display only Category A serials, when category A is selected, and category B serials when category B serials are selected. in this code, I'm saving all category A and B serials together, I cannot seem to figure out how to store this List of Serials in category A and access it back. – Patrick Aug 03 '16 at 14:04
  • Tag with `winforms` or `wpf` for future references please. – Kilazur Aug 03 '16 at 14:18

1 Answers1

2

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,

 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)

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,

outputs Hope Helps,

Community
  • 1
  • 1
Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37
  • this looks very promising!... I'm going to try this and let you know! – Patrick Aug 03 '16 at 14:41
  • @Patrick Hope helps man, I updated the answer. Check the outputs. – Berkay Yaylacı Aug 03 '16 at 14:46
  • Edited: This is working!, I had missed a "}" I just want to know how can I save category A values into database now? so it's not run-time anymore... thanks man! – Patrick Aug 03 '16 at 15:08
  • this is awesome! can I save this into database now? so when I open the application it's saved there... – Patrick Aug 03 '16 at 15:15
  • Yes you can! But you need to do some research about it. Here is a starting point for you; http://stackoverflow.com/questions/34851200/how-to-insert-listt-into-database. Glad to hear it worked! @Patrick – Berkay Yaylacı Aug 03 '16 at 15:19