将datagrid选中的值存储到数组

时间:2017-06-09 22:48:45

标签: c# arrays multidimensional-array datagridview

我正在实施一个简单的购物车,我有一个SQL存储过程,它返回所有产品并将它们存储到DataGridView中,卖方选择一个产品并输入所需数量,例如: 2.

卖家可以多次选择产品,因此每当卖家点击“添加到购物车”按钮时,我需要存储代码和所选产品的数量,这样我最终可以使用该数据来处理销售并显示总数。 (我正在想一个阵列,但如果你能建议我一个更好的解决方案,我会很感激,我希望你能帮助我。)

这是我到目前为止所拥有的: Sales Menu

“cod”是我从所选产品中检索的产品代码。

这是Process Sale按钮的代码:

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

我希望它返回的内容是这样的:

[1][20] //Which means the product with code 1 was selected with 20 units, for 
    //e.g. Aspirin 20 units

[1][20]



[2][2]

依旧......

我的问题是for循环需要一个设置其结尾的变量,在本例中为n,但是n将是填充数组的术语的长度,但在这种情况下,每次我点击添加到购物车数组将是一个更大的位置,我希望我解释自己。

0 个答案:

没有答案