Saturday, September 5, 2015 Entity Framework Fetch records and display in Gridview

In this article I am going to explain how to fetch the records from Sql server database and display in Gridview using Entity Framework in


In this tutorial select the records from Sql server database using database first approach. I have created a table Movie in database and having records in it. I am going to display the records in Gridview data control.

To set up the entity framework project read the article How to set Entity Framework project or website in

After that add a webform to project/website. Drag and drop the Gridview control from toolbox to webform.

HTML Markup of webform:
  <fieldset style="width:500px;">
            <legend>Entity Framework Tutorial</legend>              
         <asp:GridView ID="grdmovie" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
               <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <asp:BoundField DataField="Name" HeaderText="Movie Name" >
                     <ItemStyle HorizontalAlign="Center" />
                     <asp:BoundField DataField="Genre" HeaderText="Genre"> <ItemStyle HorizontalAlign="Center" />
                     <asp:BoundField DataField="Cost" HeaderText="Budget (In Crore)"> <ItemStyle HorizontalAlign="Center" />
                     <asp:ImageField DataImageUrlField="Poster" HeaderText="Poster" ControlStyle-Width="200px">
<ControlStyle Width="200px"></ControlStyle>
               <EditRowStyle BackColor="#999999" />
               <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
               <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
               <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
               <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
               <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
               <SortedAscendingCellStyle BackColor="#E9E7E2" />
               <SortedAscendingHeaderStyle BackColor="#506C8C" />
               <SortedDescendingCellStyle BackColor="#FFFDF8" />
               <SortedDescendingHeaderStyle BackColor="#6F8DAE" />

Create the object of Entity connection
DemoEntities db = new DemoEntities();

Dim db As New DemoEntities

Create a function to bind gridview and call it on page load.
protected void Page_Load(object sender, EventArgs e)
public void BindGrid()
        var bindgrid = db.Movies.Select(m => m);
        grdmovie.DataSource = bindgrid.ToList();

  Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
        End If
    End Sub
    Public Sub BindGrid()
        Dim bindgrid = db.Movies.[Select](Function(m) m)
        grdmovie.DataSource = bindgrid.ToList()
    End Sub

Build the project and run. 
Demo: Entity Framework Fetch records and display in Gridview

  In this article we have learn to how to fetch and display record using Entity Framework 6.0 in Visual studio 2013I hope you enjoyed this article.

No comments:

Post a Comment