-5

I'm creating a shopping application for which I need to transfer my selected items (via add to cart button) to a DataGridView in another Form so that it can show all the items that I've bought. For this I have designed a global function in my second Form so that whenever I press the "add to cart"-button in my first Form the values are added in the DataGridView in the second Form. But the code is not showing anything in the DataGridView so far.

Form1:

public partial class CLOTHES : Form
{
    public static int cost = 0;
    public static string name = "";
    public static string size = "";
    public static string NAME = "";
    public static string SIZE = "";
    public static int total = 0;

    public CLOTHES()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (checkBox1.Checked == true)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                SIZE = "small";
                label1.Text = "T-SHIRT";
                cost = 300;
            }
            else if (comboBox1.SelectedIndex == 1)
            {
                SIZE = "MEDIUM";
                label1.Text = "T-SHIRT";
                cost = 400;
            }
            else if (comboBox1.SelectedIndex == 2)
            {
                SIZE = "LARGE";
                label1.Text = "T-SHIRT";
                cost = 500;
            }

            name = label1.Text;
            size = comboBox1.SelectedIndex.ToString();
            cart.populatedatagridview(name, size, cost);
            nextform(cost, size, name);
        }
    }

    void nextform(int cost, string size, string name)
    {
        total = cost;
        NAME = name;
        size = SIZE;
        MessageBox.Show("total " + total);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Hide();
        cart f5 = new cart();
        f5.ShowDialog();
    }
}

Form2:

public partial class cart : Form
{
    public cart()
    {
        InitializeComponent();
    }

    public static void populatedatagridview(string name, string size, int total)
    {
        cart cs = new cart();

        string[] row = { "" + name, "" + size, "" + total };
        cs.dataGridView1.Rows.Add(row);
    }
}
Tobias Tengler
  • 6,848
  • 4
  • 20
  • 34
  • You are creating a new cart form each time and then not showing it. Please visit [help] and read [ask] – Ňɏssa Pøngjǣrdenlarp Jun 23 '19 at 15:48
  • [Duplicate 1](http://stackoverflow.com/questions/3062575/), [duplicate 2](http://stackoverflow.com/questions/7800731/), [duplicate 3](http://stackoverflow.com/questions/17032484/), [duplicate 4](http://stackoverflow.com/questions/17836398/), [duplicate 5](http://stackoverflow.com/questions/25316230/), [duplicate 6](http://stackoverflow.com/questions/29092707/) ... – Dour High Arch Jun 23 '19 at 15:56
  • Possible duplicate of [Passing Values Between Windows Forms c#](https://stackoverflow.com/questions/17836398/passing-values-between-windows-forms-c-sharp) – C-Pound Guru Jun 23 '19 at 17:17

1 Answers1

0

See code update below, just store your cart in variable, and pass to form2 function to bind your data to gridview before displaying form2

Form1:

public partial class CLOTHES : Form
{
    public static int cost = 0;
    public static string name = "";
    public static string size = "";
    public static string NAME = "";
    public static string SIZE = "";
    public static int total = 0;

    List<string[]> data = new List<string[]>();

    public CLOTHES()
    {
        InitializeComponent();
    }


    private void button1_Click(object sender, EventArgs e)
    {
        if (checkBox1.Checked == true)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                SIZE = "small";
                label1.Text = "T-SHIRT";
                cost = 300;
            }
            else if (comboBox1.SelectedIndex == 1)
            {
                SIZE = "MEDIUM";
                label1.Text = "T-SHIRT";
                cost = 400;
            }
            else if (comboBox1.SelectedIndex == 2)
            {
                SIZE = "LARGE";
                label1.Text = "T-SHIRT";
                cost = 500;
            }

            name = label1.Text;
            size = comboBox1.SelectedIndex.ToString();               
            nextform(cost, size, name);
            string[] row = { "" + name, "" + size, "" + total };
            data.Add(row);

        }
    }

    void nextform(int cost, string size, string name)
    {
        total = cost;
        NAME = name;
        size = SIZE;
        MessageBox.Show("total " + total);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Hide();
        cart f5 = new cart();
        f5.populatedatagridview(data);
        f5.ShowDialog();
    }

}

Form2:

 public partial class cart : Form
{
    public cart()
    {
        InitializeComponent();
    }

    public void populatedatagridview(List<string[]> data)
    {

        foreach (var item in data)
        {
            dataGridView1.Rows.Add(item);
        }


    }
}
Afis
  • 32
  • 1
  • 5