如何以另一种形式将值传输到DataGridView

时间:2019-06-23 15:40:35

标签: c# winforms datagridview

我正在创建一个购物应用程序,需要将其选择的商品(通过“添加到购物车”按钮)转移到另一种形式的DataGridView中,以便它可以显示我购买的所有商品。为此,我在第二个窗体中设计了一个全局函数,以便每当我在第一个窗体中按下“添加到购物车”按钮时,值就会添加到第二个窗体的DataGridView中。但是到目前为止,代码尚未在DataGridView中显示任何内容。

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);
    }
}

1 个答案:

答案 0 :(得分:0)

请参阅下面的代码更新,仅将购物车存储在变量中,并传递给form2函数以在显示form2之前将数据绑定到gridview

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);
        }


    }
}