pass value comboBox to Datagridview on difference form

MiPakTeh 1,476 Reputation points
2021-11-20T23:59:58.153+00:00

Hi All,

What try to do;

pass all value comboBox from Form1 to Datagridview on Form2.Bellow the code I test for that.

Form1:

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 Office_a3
{
    public partial class Form1 : Form
    {
        List<string> Quantity = new List<string>();
        List<string> Unit_Price = new List<string>();
        public static int qu;

        public Form1()
        {
            InitializeComponent();

            button1.Text = "Calculation";
            button2.Text = "New Form comboBox";

            //label.
            label1.Text = "Quantity";
            label1.Font = new Font("Segoe WP", 13);
            label1.Size = new Size(120, 30);
            label1.AutoSize = false;
            label1.BorderStyle = BorderStyle.FixedSingle;

            label2.Text = "Unit Price";
            label2.Font = new Font("Segoe WP", 13);
            label2.Size = new Size(120, 30);
            label2.AutoSize = false;
            label2.BorderStyle = BorderStyle.FixedSingle;

            label3.Text = "Amount";
            label3.Font = new Font("Segoe WP", 13);
            label3.Size = new Size(120, 30);
            label3.AutoSize = false;
            label3.BorderStyle = BorderStyle.FixedSingle;

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

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

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

            foreach (string item in Unit_Price)
            {
                comboBox3.Items.Add(item);
                comboBox4.Items.Add(item);
            }

            int x = 0;
            comboBox5.Text = x.ToString();
            comboBox6.Text = x.ToString();


        }

        private void button1_Click(object sender, EventArgs e)
        {
            comboBox5.Items.Clear();
            if (this.comboBox1.SelectedItem != null || this.comboBox3.SelectedIndex != -1)
            {
                double c1 = double.Parse(comboBox1.Text);
                double c2 = double.Parse(comboBox3.Text.Replace("$", "").ToString());
                var result = c1 * c2;
                comboBox5.Items.Add(result);
                comboBox5.SelectedIndex = 0;
            }
            comboBox6.Items.Clear();
            if (this.comboBox2.SelectedItem != null || this.comboBox4.SelectedIndex != -1)
            {
                double c3 = double.Parse(comboBox2.Text);
                double c4 = double.Parse(comboBox4.Text.Replace("$", "").ToString());
                var resultA = c3 * c4;
                comboBox6.Items.Add(resultA);
                comboBox6.SelectedIndex = 0;
            }



        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();  //current form will hide
            Form1 fm = new Form1(); //another form will open
            fm.Show();
        }

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

            double qu = double.Parse(comboBox1.Text);
        }



    }
}

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 Office_a3
{
    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";

            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                //double cmbvalue = double.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
                //Form1.qu = (int)cmbvalue;
                //int cmbvalue = int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
                //Form1.qu = (int)cmbvalue;
                Form1.qu = int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
            }


        }


    }
}
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2021-11-22T07:08:28.52+00:00

    @MiPakTeh , Welcome to Microsoft Q&A,

    Based on your description, you want to pass the combobox value in form1 to datagirdview in form2.

    Continue with the question calculation between two comboBox, I transfer the data in combobox3 to datagirdview.

    I used Application.Openforms to get the instance of another form.

    Form1 code:

    public partial class Form1 : Form  
    {  
        List<string> Quantity = new List<string>();  
        List<string> Unit_Price = new List<string>();  
        public static int qu;  
        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);  
      
            }  
        }  
      
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
        {  
            if (int.TryParse(comboBox1.Text, out firstvalue))  
            {  
                t = true;  
            }  
        }  
        int firstvalue;  
        bool t;  
        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"));  
            }  
        }  
      
        private void button1_Click(object sender, EventArgs e)  
        {  
            Form2 form2 = new Form2();  
            form2.Show();  
        }  
        public ComboBox cmb  
        {  
            get  
            {  
                return comboBox3;  
            }  
            set  
            {  
                comboBox3 = value;  
            }  
        }  
         
    }  
    

    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 = "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[0].Value = form.cmb.Items[i].ToString();  
                }  
      
      
            }  
      
            
        }  
    

    Result:

    151353-1.gif


    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.


1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-11-21T21:35:16.193+00:00

    In the form with the DataGridView add

    public delegate void OnPassingNumber(int value);
    public event OnPassingNumber PassingNumber;
    

    In the same form pass the value

    if (int.TryParse(dataGridView1.Rows[i].Cells[0].Value.ToString(), out var qu))
    {
        PassingNumber?.Invoke(qu);
    }
    

    In the calling form, subscribe to PassingNumber which looks like this and in the body value is the value from Rows[i].Cells[0].Value, use it to assign to whatever you need in the calling form.

    private void ChildFormOnPassingNumber(int value)
    {
    
    }
    

    See a simple demo in this repository.

    0 comments No comments

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.