@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:
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.