1

Goal in mind, the concept is kind of like a shopping cart, so as they add items to list(Detail) it keeps the items they are adding in memory. This works when ever I first load the list(grid) and add more rows. But if I set the first row and set the item and price and then decide to add 3 more rows then the info I had added gets deleted instead of keeping its values and just load more lines to the list which would repopulate the gridview. In the past I have done this with datatables but I want to be able to move from that and use this List Class Also I have it set as viewstate so I can use it through out my page.

private ListArDocumentdetail Detail
{
    get
    {
        ListArDocumentdetail _detail = new ListArDocumentdetail();
        if (ViewState["Detail"] != null)
        {
            _detail = (ListArDocumentdetail)ViewState["Detail"];    
        }
        return _detail;
     }
     set
     {
        ViewState["Detail"] = value;
     }
}
protected void Page_Load(object sender, EventArgs e)
{
    //creates 2 rows to start off
    CreateRows(2);
}
public void CreateRows(int rowstoadd)
{
    int newtotalrows = Detail.Count + rowstoadd - 1;
    for (int i = Detail.Count; i <= newtotalrows; i++)
    {
        ArDocumentdetail detail = new ArDocumentdetail();
        detail.Lineid = i;
        detail.Itemid = 0;
        detail.Quantity = 1;
        if (Detail.Count > 0)
            Detail.Insert(Detail.Count, detail);
        else
            Detail.Add(detail);

        Detail = Detail;
    }
    gvInvoiceDetail.DataSource = Detail;
    gvInvoiceDetail.DataBind();

    GridViewRow row = gvInvoiceDetail.Rows[gvInvoiceDetail.Rows.Count - 1];
    ImageButton btnAdd = (ImageButton)row.FindControl("btnAdd");
    btnAdd.Visible = true;
}
protected void ibAdd_Click(object sender, ImageClickEventArgs e)
{
    //user can type in how many rows they want to add on to current amount of rows
    //so since grid starts off at 2 and they type 3 the grid refreshes with 5 rows.
    CreateRows(Convert.ToInt32(txtRows.Text));
}

protected void UpdateRow(object sender, EventArgs e)
{
    ImageButton btnUpdate = sender as ImageButton;
    GridViewRow row = btnUpdate.NamingContainer as GridViewRow;

    TextBox txtPrice = (TextBox)row.FindControl("txtPrice");
    TextBox txtQuantity = (TextBox)row.FindControl("txtQuantity");
    DropDownList ddlDescription = (DropDownList)row.FindControl("ddlDescription");

    int index = Detail.FindIndex(f => f.Lineid == row.RowIndex);
    Detail[index].Itemid = Convert.ToInt32(ddlDescription.SelectedValue);
    Detail[index].Price = Convert.ToDecimal(txtPrice.Text);
    Detail[index].Subtotal = Convert.ToDecimal(Detail[index].Price * Convert.ToInt32(txtQuantity.Text));

}

1 Answer 1

1

I can suggest you the logic:

  1. Push a list into viewstate say Viewstate["List"],
  2. Let a user chose an item. Then List list = (List)Viewstate["List"];
  3. Add the selected item to List list. i.e. list.Add(item);
  4. Now push the item back to viewstate. Viewstate["list"] = list;
  5. Bind it to grid or display it on page. Whatever you want.
Sign up to request clarification or add additional context in comments.

3 Comments

beautiful man thanks! another note was that in my gridview rowdatabound I wasn't setting the values from the list in viewstate...that's my bad..
Sorry , I did'nt get you. Could you elaborate your situation.
Your logic help me add/update correctly but what I was doing wrong was when databinding my grid I was setting the values of the controls correctly..like my dropdownlist for items i wasn't grabbing the value stored in the list so it was always showing blank..thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.