Hi ahsan.ali,
Refer the below sample.
HTML
<asp:GridView runat="server" ID="gvCategories" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvCategories.DataSource = GetCategory();
gvCategories.DataBind();
}
}
public static List<ProductCategory> GetCategory()
{
NorthwindModel.NorthwindEntities dc = new NorthwindModel.NorthwindEntities();
//var q = dc.ProductCategories.Select(x => new { x.CategoryName, x.CategoryID });
var q = from x in dc.ProductCategories
select new ProductCategory() { ID = x.CategoryID, Name = x.CategoryName };
List<ProductCategory> lstproductCatogry = q.ToList();
return lstproductCatogry;
}
public class ProductCategory
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
Output
ID | Name |
1 |
Beverages |
2 |
Condiments |
3 |
Confections |
4 |
Dairy Products |
5 |
Grains/Cereals |
6 |
Meat/Poultry |
7 |
Produce |
8 |
Seafood |