0

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!.

1
  • use for instead of foreach so you can take the amount value by it's index. Commented Jun 28, 2020 at 15:51

1 Answer 1

1

I finally did it, use foreach for each cell.

    int index = 0;
    foreach (string x in item1)
    {
        dataGridView1.Rows[index].Cells[0].Value = x;
        index++;
    }

    int index1 = 0;
    foreach (string x2 in item2)
    {
        dataGridView1.Rows[index1].Cells[1].Value = x2;
        index1++;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.