Hi makenzi.exc,
Database
CREATE TABLE [dbo].[Vegetables](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[PricePerKg] [varchar](50) NULL,
[Quantity] [int] NULL,
CONSTRAINT [PK_Vegetables] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
HTML
<asp:GridView ID="gvVegetables" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="PricePerKg" HeaderText="PricePerKg" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" />
</Columns>
</asp:GridView>
Namespace
using System;
using System.Linq;
using System.Web.UI.WebControls;
Code
#region Events
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
#endregion
#region Private Methods
/// <summary>
/// Populate GridView
/// </summary>
private void BindGrid()
{
{
VegetablesEntities entities = new VegetablesEntities();
var vegetable = from p in entities.Vegetables
select new
{
Id = p.ID,
Name = p.Name,
PricePerKg = p.PricePerKg,
Quantity = p.Quantity,
};
gvVegetables.DataSource = vegetable.ToList();
gvVegetables.DataBind();
}
}
#endregion
Screenshot
