// updates table private void RefreshCartTable() { // get cart ShoppingCart_S1 _cart = (ShoppingCart_S1)((ShopMaster)Master).GetCart(); // cycle through each row, except the first one // find items w/ quantity of zero, remove row TableRowCollection _rows = tableCart.Rows; int i = 1; while (i < _rows.Count) { TableRow _row = _rows[i]; // get ID for row HyperLink _link = (HyperLink)_row.Cells[0].Controls[0]; int _id = Int32.Parse(_link.Text); int _actualQuantity = _cart.Quantity(_id); // check quantity if (_actualQuantity <= 0) { _row.Cells.Clear(); _rows.Remove(_row); Trace.Warn("cart", string.Format("removed row id: {0}", _id)); } else { // check if quantity changed int _quantity = Int32.Parse((_row.Cells[3].Text)); if (_actualQuantity != _quantity) { _row.Cells[3].Text = _actualQuantity.ToString(); } ++i; } } // check row count if (tableCart.Rows.Count <= 1) { // empty table tableCart.Rows.Clear(); labelCart.Text = "Cart is empty"; labelCart.ForeColor = System.Drawing.Color.OrangeRed; tableCartContainer.Visible = false; } }
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding) { int rows = base.CreateChildControls(dataSource, dataBinding); // no data rows created, create empty table if enabled if (rows == 0 && (ShowFooterWhenEmpty || ShowHeaderWhenEmpty)) { // create the table Table table = CreateChildTable(); Controls.Clear(); Controls.Add(table); DataControlField[] fields; if (AutoGenerateColumns) { var source = new PagedDataSource { DataSource = dataSource }; ICollection autoGeneratedColumns = CreateColumns(source, true); fields = new DataControlField[autoGeneratedColumns.Count]; autoGeneratedColumns.CopyTo(fields, 0); } else { fields = new DataControlField[Columns.Count]; Columns.CopyTo(fields, 0); } TableRowCollection newRows = table.Rows; if (ShowHeaderWhenEmpty) { // create a new header row _headerRow = CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal); InitializeRow(_headerRow, fields, newRows); } // create the empty row GridViewRow emptyRow = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal); TableCell cell = new TableCell(); List <DataControlField> f = fields.Where(dataControlField => dataControlField.Visible).ToList(); cell.ColumnSpan = f.Count; //cell.Width = Unit.Percentage(100); // respect the precedence order if both EmptyDataTemplate // and EmptyDataText are both supplied ... if (EmptyDataTemplate != null) { EmptyDataTemplate.InstantiateIn(cell); } else if (!string.IsNullOrEmpty(EmptyDataText)) { cell.Controls.Add(new LiteralControl(EmptyDataText)); } emptyRow.Cells.Add(cell); GridViewRowEventArgs e = new GridViewRowEventArgs(emptyRow); OnRowCreated(e); newRows.Add(emptyRow); emptyRow.DataBind(); OnRowDataBound(e); emptyRow.DataItem = null; if (ShowFooterWhenEmpty && ShowFooter) { // create footer row _footerRow = CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal); InitializeRow(_footerRow, fields, newRows); newRows.Remove(emptyRow); } } return(rows); }
// updates table private void RefreshCartTable() { // get cart ShoppingCart_S2 _cart = (ShoppingCart_S2)((ShopMaster)Master).GetCart(); // cycle through each row, except the first one // find items w/ quantity of zero, remove row TableRowCollection _rows = tableCart.Rows; TableRow _row = null; int i = 1; while (i < _rows.Count) { _row = _rows[i]; // get ID for row HyperLink _link = (HyperLink)_row.Cells[0].Controls[0]; int _id = Int32.Parse(_link.Text); Trace.Write("Cart", string.Format("Refreshing row id: {0}", _id)); int _actualQuantity = _cart.Quantity(_id); // check quantity if (_actualQuantity <= 0) { _row.Cells.Clear(); _rows.Remove(_row); Trace.Warn("cart", string.Format("removed row id: {0}", _id)); } else { // always refresh int _quantity = Int32.Parse((((TextBox)(_row.Cells[4].Controls[0])).Text)); Trace.Write("Cart", string.Format("Quantity read: {0}", _quantity)); // get item info Item_S2 _item = (Item_S2)((ShopMaster)Master).GetItem(_id); // update quantity and ext price/weight //_row.Cells[4].Text = _actualQuantity.ToString(); _row.Cells[5].Text = (_item.Price * _actualQuantity).ToString("C2"); _row.Cells[6].Text = (_item.Weight * _actualQuantity).ToString("f2"); ++i; } } // check row count if (tableCart.Rows.Count <= 1) { DisplayEmptyCart(); return; } // calculate totals double _totalPrice = 0, _totalWeight = 0; foreach (ShoppingCart_S2.ShoppingCartItem_S2 _cartItem in _cart.Items) { // get item Item_S2 _item = (Item_S2)((ShopMaster)Master).GetItem(_cartItem.ItemID); _totalPrice += _item.Price * _cartItem.Quantity; _totalWeight += _item.Weight * _cartItem.Quantity; } // add totals row _row = new TableRow(); _row.CssClass = "TotalRow"; // populate empty cells TableCell _cell = null; for (i = 0; i < 5; ++i) { _cell = new TableCell(); _cell.CssClass = "EmptyCell"; _cell.Text = " "; _row.Cells.Add(_cell); } // total ext price _cell = new TableCell(); _cell.CssClass = "EmptyCell"; _cell.Text = _totalPrice.ToString("C2"); _row.Cells.Add(_cell); // total ext weight _cell = new TableCell(); _cell.CssClass = "EmptyCell"; _cell.Text = _totalWeight.ToString("f2"); _row.Cells.Add(_cell); tableCart.Rows.Add(_row); }