首页 > 解决方案 > How to collect values into an array and pass the collection to datagridview in C#?

问题描述

I am working on a custom POS application in Asp.Net using C# where I have two DataGridViews on a windows form. The DataGridView1 is showing all sales. When a user clicks on any record in DataGridView1, certain details like ProductName, Quantity, Variation, UnitPrice and TotalCost from the selected record on DataGridView is passed to the DataGridView2. This is done using the CurrentCellChanged event in DataGridView2.

My problem is that I have more than one details related to each sale which I am trying to show on DataGridView2 but I can't set each detail of a selected sale as the DataSource for DataGridView2. Here is the code I have tried so far:

private void DataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
    try
    {
        string saleId = DataGridView1.CurrentRow.Cells[0].Value.ToString();
        DataGridView2.DataSource = SalesClass.SelectedSaleDetails(saleId);
    }
    catch (Exception)
    {

    }
}

Here, the CurrentCellChanged event pass the selected SaleId to my SalesClass where I am querying sales details related to this specific SaleId, like this:

public static DataTable SelectedSaleDetails(string saleId)
{
    DataTable dt = new DataTable();
    dt = DataAccess.Select(string.Format("select * from vSalesDetails where SaleID = {0}", saleId));
    string[] values;

    foreach (DataRow drItem in dt.Rows)
    {
        string variation = drItem["ProductVariation"].ToString();


        string productName = drItem["ProductName"].ToString();
        decimal quantity = Convert.ToDecimal(drItem["SaleQuantity"]);
        decimal saleCostLevelC = Convert.ToDecimal(drItem["LevelCCost"]);
        decimal purchaseCostLevelC = Convert.ToDecimal(drItem["LevelCPurchaseCost"]);
        decimal levelBStandard = Convert.ToDecimal(drItem["LevelBStandard"]);
        decimal levelCStandard = Convert.ToDecimal(drItem["LevelCStandard"]);

        decimal parentTotalSalesCost = 0;
        decimal levelATotalSalesCost = 0;
        decimal levelBTotalSalesCost = 0;
        decimal levelCTotalSalesCost = 0;
        decimal salecost = 0;
        decimal unitCost = 0;
        switch (variation)
        {
            case "Parent Product":
                decimal soleProductUnitPrice = Convert.ToDecimal(drItem["SoleProductUnitPrice"]);

                salecost = quantity * soleProductUnitPrice;
                unitCost = soleProductUnitPrice;
                break;

            case "Level A":
                decimal levelBSaleCost = levelCStandard * saleCostLevelC;
                decimal levelASaleCost = levelBStandard * levelBSaleCost;

                salecost = quantity * levelASaleCost;
                unitCost = levelASaleCost;
                break;

            case "Level B":
                decimal saleCostLevelB = levelCStandard * saleCostLevelC;
                salecost = quantity * saleCostLevelB;
                unitCost = saleCostLevelB;
                break;

            case "Level C":
                salecost = quantity * saleCostLevelC;
                unitCost = saleCostLevelC;
                break;

            default:
                break;
        }

        values = new string[] {productName, variation, unitCost.ToString(), quantity.ToString(), salecost.ToString()};
    }

//  return each of the productName, variation, unitCost and quantity of a selected Sale here. This is going to be assigned as the DataGridView2 DataSource.
}

My code is working fine and I am able to get all the required values in values array within the foreach loop. However, I am somehow not able to pass sale details (productName, variation, unitCost and quantity) of a specific sale back to the DataGridView2 DataSource here:

DataGridView2.DataSource = //values returned from SalesClass;

Is there anyone who can suggest me a better yet efficient approach for this as I am a newbie here.

标签: c#arrayswinformsdatagridviewdatatable

解决方案


您是否查看过DataGridView的文档?看起来您需要在声明的BindingSource旁边声明 aDataGridView1DataGridView2

private BindingSource bindingSource1 = new BindingSource();

然后将您的代码更改为

string saleId = DataGridView1.CurrentRow.Cells[0].Value.ToString();
bindingSource1.DataSource = SalesClass.SelectedSaleDetails(saleId);   
DataGridView2.DataSource = bindingSource1;

不要string[]SalesClass.SelectedSaleDetails. 您想退回您的DataTable dt.


推荐阅读