Question

I am having problem with GridView on row command and stack up the data in another GridView:

private List<DistributionStandardPackingUnitItems> tempDistSPUI
{
    get
    {
        if (ViewState["tempDistSPUI"] == null)
        {
            return new List<DistributionStandardPackingUnitItems>();
        }
        else
        {
            return (List<DistributionStandardPackingUnitItems>)ViewState["tempDistSPUI"];
        }
    }
    set
    {
        ViewState["tempDistSPUI"] = value;
    }
}

protected void gvSPU_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);

    //Get selected standard packing name
    int rowNo = int.Parse(e.CommandArgument.ToString());
    SPUname = this.gvSPU.DataKeys[rowNo].Value.ToString();
    lblSPUname.Text = SPUname;

    //Get the record from view state
    itemList = tempDistSPUI;

    itemList = packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname);
    gvFinalised.DataSource = itemList;
    gvFinalised.DataBind();

    //Save the last record to view state
    this.tempDistSPUI = itemList;
}

Let's say when I first selected a row from gvSPU, it returns an itemList filled with data and display in gvFinalised. What I am trying to do is if I selected another row from gvSPU, the previous records in gvFinalised will still there and stack up another itemList from the secondly selected row instead of wiping up the record previously and display the latest itemList data.

I am using viewState but it does not work.

EDIT

protected void lbnAdd_Click(object sender, EventArgs e)
    {
        List<DistributionStandardPackingUnitItems> prodVariantDetail = new List<DistributionStandardPackingUnitItems>();

        int packagesNeeded = prodPackBLL.getPackagesNeededByDistributionID(distributionID);

        // get the last product variant IDs from ViewState
        prodVariantIDList = this.SelectedVariantDetailIDs;

        foreach (RepeaterItem ri in Repeater1.Items)
        {
            GridView gvProduct = (GridView)ri.FindControl("gvProduct");
            foreach (GridViewRow gr in gvProduct.Rows)
            {
                CheckBox cb = (CheckBox)gr.FindControl("cbCheckRow");
                //Prevent gvFinalised to store duplicate products
                if (cb.Checked && !prodVariantIDList.Any(i => i == gvProduct.DataKeys[gr.RowIndex].Value.ToString()))
                {
                    // add the corresponding DataKey to idList
                    prodVariantIDList.Add(gvProduct.DataKeys[gr.RowIndex].Value.ToString());
                }
            }
        }

        for (int i = 0; i < prodVariantIDList.Count; i++)
        {
            prodVariantDetail.Add(packBLL.getProdVariantDetailByID(prodVariantIDList[i]));
        }

        //Check if itemList and prodVariantDetail list contains any duplicate records
        var Gdupes = itemList.GroupBy(x => new { x.id }).Where(x => x.Skip(1).Any()).ToList();
        List<DistributionStandardPackingUnitItems> dupes = Gdupes.SelectMany(x => x).ToList();
        prodVariantDetail = itemList.Except(dupes).ToList();

        gvFinalised.DataSource = prodVariantDetail;
        gvFinalised.DataBind();

        foreach (GridViewRow gr in gvFinalised.Rows)
        {
            //Get the product packaging quantity by productName
            string name = gr.Cells[1].Text;
            int productQuantity = packBLL.getProductQuantityByName(name, distributionID);
            TextBox tb = (TextBox)gr.Cells[5].FindControl("tbQuantity");

            if (productQuantity == 0)
            {
                tb.Text = productQuantity.ToString();
            }
            else
            {
                tb.Text = (productQuantity / packagesNeeded).ToString();
            }
        }

        // save prodVariantIDList to ViewState
        this.SelectedVariantDetailIDs = prodVariantIDList;
    }

private List<string> SelectedVariantDetailIDs
    {
        get
        {
            if (ViewState["SelectedVariantDetailIDs"] == null)
            {
                return new List<string>();
            }
            else
            {
                return (List<string>)ViewState["SelectedVariantDetailIDs"];
            }
        }
        set
        {
            ViewState["SelectedVariantDetailIDs"] = value;
        }
    }
Was it helpful?

Solution

The problem is the following lines:

//Get the record from view state
itemList = tempDistSPUI; 

// here itemList will be replaced 
itemList = packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname);

First you assign tempDistSPUI from ViewState to itemList, but then you replace itemList at the next line. You need to add the elements returned from packBLL.getAllDistSPUItemByDistributionIDnSPUName to itemList instead of replacing itemList. Here's what I would do using List.AddRange Method:

itemList = tempDistSPUI; 

// add the returned elements to itemList
itemList.AddRange(packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname));

UPDATE
To prevent duplication with the previous elements:

itemList = tempDistSPUI; 

List<DistributionStandardPackingUnitItems> itemListNew = new List<DistributionStandardPackingUnitItems>();
itemListNew = packBLL.getAllDistSPUItemByDistributionIDnSPUName(distributionID, SPUname);

// get all previous IDs in a List<int>
List<int> previousIDs = itemList.Select(x => x.id).ToList();

// filter itemListNew and add the elements to itemList
itemList.AddRange(itemListNew.Where(x => !previousIDs.Contains(x.id));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top