C# 将datagrid选定值存储到数组中
我正在实现一个简单的购物车,我有一个SQL存储过程,返回所有产品并将其存储到DataGridView中,卖家选择一个产品并键入所需数量,例如2 卖家可以多次选择产品,因此每次卖家单击“添加到购物车”按钮时,我都需要存储代码和所选产品的数量,以便最终我可以使用该数据处理销售并显示总额。(我正在考虑阵列,但如果您能为我提供更好的解决方案,我将不胜感激,希望您能帮助我) 以下是我目前掌握的情况: “cod”是我从所选产品检索的产品代码 这是“处理销售”按钮中的代码:C# 将datagrid选定值存储到数组中,c#,arrays,multidimensional-array,datagridview,C#,Arrays,Multidimensional Array,Datagridview,我正在实现一个简单的购物车,我有一个SQL存储过程,返回所有产品并将其存储到DataGridView中,卖家选择一个产品并键入所需数量,例如2 卖家可以多次选择产品,因此每次卖家单击“添加到购物车”按钮时,我都需要存储代码和所选产品的数量,以便最终我可以使用该数据处理销售并显示总额。(我正在考虑阵列,但如果您能为我提供更好的解决方案,我将不胜感激,希望您能帮助我) 以下是我目前掌握的情况: “cod”是我从所选产品检索的产品代码 这是“处理销售”按钮中的代码: private void btn
private void btnCarrito_Click(object sender, EventArgs e){
ProcessSale s = new ProcessSale();
if (cod != "")
{
if (txtQuantity.Text != "")
{
MessageBox.Show(s.AddToCart(Convert.ToInt32(cod),
Convert.ToInt32(txtQuantity.Text)), "", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
MessageBox.Show("You need to sell at least 1 unit", "",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("You need to select a product", "",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}}
这是ProcessSale的代码:
public string AddToCart(int cod, int quantity)
{
string message= "";
//Cleary this n=0 is wrong and the for is not going to work
//I don't know what to set n because the Array is going to increase
//every time the seller adds a product to the cart
int n = 0;
int[][] array = new int[2][];
for (int i = 1;i<=n;i++)
{
array[n][n] = cod;
array[n][n+1] = quantity;
n++;
}
// I believe it should return the Array
return array;
}
等等
我的问题是for循环需要一个设置其结束的变量,在本例中,n是填充数组的术语长度,但在本例中,每次单击“添加到购物车”时,数组都会大一个位置,我希望我自己解释一下。您的问题内容很好,除非您从未问过任何问题。你到底在想什么?对不起,罗伯特。我的问题是for循环需要一个设置其结束的变量,在本例中为n,但n将是填充数组的术语的长度,但在本例中,每次单击add to cart时,数组将比我自己解释的大1个位置。您可以使用比多维数组更好的数据结构(这不是80年代),这使事情更清楚、更容易处理。创建一个
SalesOrder
类,例如有两个属性:Cod
和Quantity
,然后创建一个集合(例如列表
)当有人单击ProcessSale
按钮时,处理这些销售订单。处理DataGrid
的每一行,并决定是否需要将新的SalesOrder
添加到列表中,或只是增加现有订单的数量(因为该行的Cod
与List@Aceman你很好,但是如果你愿意的话,请编辑你的答案来澄清这一点。谢谢你,马什顿和罗伯特,我现在就试着去做。
[1][20] //Which means the product with code 1 was selected with 20 units, for
//e.g. Aspirin 20 units
[1][20]
[2][2]