Click here to Skip to main content
15,891,738 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
How can I add column to ASP.net ListView?


    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Product.aspx.cs"     Inherits="ListViewProduct.Product" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <link href="Content/bootstrap.css" rel="stylesheet" />
    <link href="Content/bootstrap-theme.css" rel="stylesheet" />
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
        <asp:ListView ID="ListView1" runat="server"
            SelectMethod="GetProducts"
            ItemType="ListViewProduct.Product"
            DataKeyNames="ProductID">

            <LayoutTemplate>
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>ProductName</th><th>UnitsInStock</th>    <th>UnitPrice</th><th>New price</th><th>TestTotalPrice</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr id="itemPlaceholder" runat="server"></tr>
                    </tbody>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                    <tr>
                        <td><%#Item.ProductName %></td>
                        <td><%#Item.UnitsInStock %></td>
                        <td><%#Item.UnitPrice %></td>
                        <td><%#Item.NewPrice %></td>
                        <td><%#Item.TestTotalPrice %></td>
                    </tr>
            </ItemTemplate>



            </asp:ListView>
    </div>

    </form>
</body>
</html>




namespace ListViewProduct
{
    public partial class Product : System.Web.UI.Page
    {
        NorthwindEntities db = new NorthwindEntities();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public IEnumerable<Product> GetProducts()
        {
            return db.Products;
        }
    }
}




    namespace ListViewProduct
    {
    using System;
    using System.Collections.Generic;

    public partial class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public Nullable<int> SupplierID { get; set; }
        public Nullable<int> CategoryID { get; set; }
        public string QuantityPerUnit { get; set; }
        public Nullable<decimal> UnitPrice { get; set; }
        private Nullable<decimal> newPrice ;

        public Nullable<decimal> NewPrice
        {
            get {
                if (UnitPrice > 80)
                {
                    newPrice = UnitPrice * 0.9m;
                }
                else
                {
                    newPrice = UnitPrice;
                }
                return newPrice;
            }
            set { newPrice = value; }
        }


        // get { if (condition) { return a; } else { return b;} }

        public Nullable<short> UnitsInStock { get; set; }
        public Nullable<short> UnitsOnOrder { get; set; }
        public Nullable<short> ReorderLevel { get; set; }
        public bool Discontinued { get; set; }
        public Nullable<decimal> TotalPrice { get; set; }
        //public Nullable<decimal> TestTotalPrice { get; set; }
        private Nullable<decimal> testTotalPrice;

        public Nullable<decimal> TestTotalPrice
        {
            get { return testTotalPrice; }
            set { testTotalPrice = UnitsInStock * UnitPrice; }
        }

    }
}
Posted

1 solution

C#
lvRegAnimals.Columns.Add("Id");
lvRegAnimals.Columns.Add("Name");
lvRegAnimals.Columns.Add("Age");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900