Loop through comboBox

MiPakTeh 1,476 Reputation points
2021-11-23T23:54:48.613+00:00

Hi All,
Actually this question continue from the pass thread.I have 3 comboBox on form1 for input data then with proceed click button1
DataGridView will be show the result.My problem is the code Loop read all the comboBox item.I need only the item user key in.

form1;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataGridView_3
{
    public partial class Form1 : Form
    {
        List<string> Quantity = new List<string>();
        List<string> Unit_Price = new List<string>();
        NumberStyles currencyStyle = NumberStyles.Currency;
        NumberFormatInfo numberFormat = CultureInfo.CurrentCulture.NumberFormat;
        public Form1()
        {
            InitializeComponent();

            for (int i = 1; i < 100; i++)
                Quantity.Add(i.ToString());

            foreach (string item in Quantity)
            {
                comboBox1.Items.Add(item);

            }

            for (int i = 50; i < 500; i++)
                Unit_Price.Add(i.ToString("$#,##0.00"));

            foreach (string item in Unit_Price)
            {
                comboBox2.Items.Add(item);

            }
        }

        public ComboBox cmb
        {
            get
            {
                return comboBox3;
            }
            set
            {
                comboBox3 = value;
            }
        }
        public ComboBox cmb_
        {
            get
            {
                return comboBox2;
            }
            set
            {
                comboBox2 = value;
            }
        }
        public ComboBox cmb_1
        {
            get
            {
                return comboBox1;
            }
            set
            {
                comboBox1 = value;
            }
        }

        int firstvalue;
        bool t;
        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (int.TryParse(comboBox1.Text, out firstvalue))
            {
                t = true;
            }
        }

        private void comboBox2_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (double.TryParse(comboBox2.Text, currencyStyle, numberFormat, out var secondvalue) && t)
            {
                double Result_ = firstvalue * secondvalue;
                comboBox3.Items.Add(Result_.ToString("$#,##0.00"));
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
        }
    }
}

form2;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataGridView_3
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Quantity";
            dataGridView1.Columns[1].Name = "Unit Price";
            dataGridView1.Columns[2].Name = "Amount";

            Form1 form = (Form1)Application.OpenForms["Form1"];
            dataGridView1.Rows.Add(form.cmb.Items.Count);
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                dataGridView1.Rows[i].Cells[2].Value = form.cmb.Items[i].ToString();
                dataGridView1.Rows[i].Cells[1].Value = form.cmb_.Items[i].ToString();
                dataGridView1.Rows[i].Cells[0].Value = form.cmb_1.Items[i].ToString();

            }

        }
    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,286 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,641 Reputation points Microsoft Vendor
    2021-11-24T02:08:51.78+00:00

    @MiPakTeh , If you want to transfer three combobox data to datagirdview, I recommend that you use list to do it because it will save some code.

    We could define the following class.

    public class Example  
        {  
            public int Qua { get; set; }  
      
            public string Pri { get; set; }  
      
            public string Result { get; set; }  
        }  
    

    Then, here is a code example you could refer to.

    Form1 code:

     public partial class Form1 : Form  
        {  
      
            List<string> Quantity = new List<string>();  
            List<string> Unit_Price = new List<string>();  
            NumberStyles currencyStyle = NumberStyles.Currency;  
            NumberFormatInfo numberFormat = CultureInfo.CurrentCulture.NumberFormat;  
            public Form1()  
            {  
                InitializeComponent();  
                for(int i = 1; i < 100; i++)  
                     Quantity.Add(i.ToString());  
      
                foreach (string item in Quantity)  
                {  
                    comboBox1.Items.Add(item);  
      
                }  
      
                for (int i = 50; i < 500; i++)  
                    Unit_Price.Add(i.ToString("$#,##0.00"));  
      
                foreach (string item in Unit_Price)  
                {  
                    comboBox2.Items.Add(item);  
      
                }  
            }  
      
            public List<Example> list { get; set; }  
              
      
            int firstvalue;  
            bool t;  
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
            {  
                if (int.TryParse(comboBox1.Text, out firstvalue))  
                {  
                    t = true;  
                }  
            }  
      
            private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)  
            {  
                if (double.TryParse(comboBox2.Text, currencyStyle, numberFormat, out var secondvalue) && t)  
                {  
                    double Result_ = firstvalue * secondvalue;  
                    comboBox3.Items.Add(Result_.ToString("$#,##0.00"));  
                }  
      
                comboBox3.SelectedIndex = comboBox3.Items.Count - 1;  
                 
      
                list.Add(new Example { Qua = Convert.ToInt32(comboBox1.SelectedItem), Pri = comboBox2.SelectedItem.ToString(), Result = comboBox3.SelectedItem.ToString() });  
      
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                Form2 form2 = new Form2();  
                form2.Show();  
            }  
      
            private void Form1_Load(object sender, EventArgs e)  
            {  
                list = new List<Example>();  
            }  
        }  
    

    Form2 code:

     public partial class Form2 : Form  
        {  
            public Form2()  
            {  
                InitializeComponent();  
                dataGridView1.ColumnCount = 3;  
                dataGridView1.Columns[0].Name = "Quantity";  
                dataGridView1.Columns[1].Name = "Unit Price";  
                dataGridView1.Columns[2].Name = "Result";  
      
                Form1 form = (Form1)Application.OpenForms["Form1"];  
                dataGridView1.Rows.Add(form.list.Count);  
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)  
                {  
                    dataGridView1.Rows[i].Cells[0].Value = form.list[i].Qua.ToString();  
                    dataGridView1.Rows[i].Cells[1].Value = form.list[i].Pri.ToString();  
                    dataGridView1.Rows[i].Cells[2].Value = form.list[i].Result.ToString();  
      
                }  
            }  
        }  
    

    Result:

    152031-10.gif

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.