DbSet Result displaying in ASP.NET Grid-Like Format

How to Display Data in Grid-Like Format

In the previous article, we had seen how to iterate through a database object in code and retrieve its values. This is possible with the "mapping" established by the Entity Framework, where an Object-Relational Mapper is served up as an intermediary which maps our data from the database and turn it to class and objects that allows it to work with it in code. Now we would use a gridview to populate out our data from the database.

Step 1: Place a GridView server Control in Default.aspx of the previous article

Right inside the Toolbox select Data > GridView drag and drop it and set up the programmatic ID to customerGridView.


Note: GridView Data Control has two special non-visual  elements  that are Data Source and Data Bind; Data Source ask the question on "how do I get the Data?" , while Data-Bound ask the question of "how do I connect to the database, retrieve the data within and display it to the end user?".


Step 2:  Write code in the Default.aspx.cs that retrieves data in Grid-Like format

In the previous article, we iterated through the customer Object and retrieved its data. Instead of iterating we are going to assign the contents of this customer to the gridview data source:


namespace LocalDbsample
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SkypletEntities db = new SkypletEntities();
            var customers = db.Customers;
            //string result = "";
            //foreach (var customer in customers)
            // {
            // result += string.Format("{0} | {1} | {2}", customer.Name, customer.CustomerId, customer.Address);
            // }
               customerGridView.DataSource = customers.ToList(); // Using Gridview to get data from database


               customerGridView.DataBind();
  
            //resultLabel.Text = result;
        }
    }
}


Step 3: Omission of ToList() on "customers" gives an error(Convert Data to a List)

customerGridView.DataSource = customers.ToList();
It's as if our "customer" object is an array like or  List< > but it is of that of type Dbset< > which is List-Like container format for the whole database. 


Even right within the Vs IDE you can hover your mouse over "customers"  and it would be shown as a Dbset< > and not of type List< >. 



Therefore while assigning the "customers" object to the GridView.DataSource it must be converted from DbSet< > to a List< > like type.

Step 4: Data binding to the GridView in code
After a data source as been established then we need to bind to it by calling the DataBind() method directly on the object:

       customerGridView.DataBind();

Save and Run:
https://www.maxybyte.com/2017/11/dbset-result-displaying-in-aspnet.html
Display Data in Asp.net grid-like format










1 comment:

Note: only a member of this blog may post a comment.

New Post

New style string formatting in Python

In this section, you will learn the usage of the new style formatting. Learn more here . Python 3 introduced a new way to do string formatti...