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