【发布时间】: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);
}
}
【问题讨论】:
-
您每次都在创建一个新的购物车表单,然后没有显示它。请访问help center 并阅读How to Ask
标签: c# winforms datagridview