Datagridview Form1 Bill: https://i.sstatic.net/LHq0V.jpg
ProductName | Amount |
Cocacola,Pepsi,TeaTH | 1,2,3 |
I have one row represent receipt from one customer(one receipt stored multiple name of product, amount each product,.. split by ',' in one cell), when double click to datagridview it will pass data to form2 and display as full detail.
I want to split ProductName, Amount into multiple row like this.
Datagridview Form2 Bill:
ProductName | Amount |
Cocacola | 1 |
Pepsi | 2 |
Tea | 3 |
This is the closet result i can get
*Form 1:*
public static string ProductName= "";
public static string ProductAmount= "";
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentRow.Index != -1)
{
ProductName= dataGridView1.CurrentRow.Cells[0].Value.ToString();
ProductAmount= dataGridView1.CurrentRow.Cells[1].Value.ToString();
}
}
*Form 2:*
string StringProductName = Form1.ProductName;
string StringProductAmmout = Form1.ProductAmount;
string[] item1 = StringProductName .Split(',');
string[] item2 = StringProductAmmout .Split(',');
foreach (string x in item1)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = x;
foreach (string x2 in item2 )
{
dataGridView1.Rows[n].Cells[1].Value = x2;
}
}
Result:
ProductName | Amount |
Cocacola | 3 |
Pepsi | 3 |
Tea | 3 |
The amount column is incorrect, it must be 1, 2, 3. Hope someone can give me an idea or any other method to solve this problem, thank you!.
forinstead offoreachso you can take the amount value by it's index.