【问题标题】:How to transfer values to DataGridView in another Form如何将值传输到另一个表单中的 DataGridView
【发布时间】:2019-11-05 14:27:45
【问题描述】:

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

表格1:

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

表格2:

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

【问题讨论】:

标签: c# winforms datagridview


【解决方案1】:

查看下面的代码更新,只需将您的购物车存储在变量中,然后传递给 form2 函数以在显示 form2 之前将您的数据绑定到 gridview

表格1:

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

}

表格2:

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

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

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


    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多