Asp.Net GridView Part 2

Assalamualaykum Wr Br…In this post we would explore the ways to bind the GridView with database. First is quite common where we explore through wizard by which you observe bind attributes and the other is by code where you need to change the Bind attributes to asp:templatefield followed by the respective attributes.
The best practice by code is to define and implement a method that fetches the data from DB and binds it to GridView. Call that method into the page_load event with not post back check. this populates the data on to the gridview at page load…lets do the poc on it…
The above image depicts two methods i.e. Page_Load and filldata methods. The First statement of Page_Load method is if constraint which checks the page is either requested for first time or being requested multiple. When Constraint is true then calls filldata method.
In this method, connection is established with the sqldatasource and sqldataadapter, which executes the command, fetches the data and fills in Dataset and the same is provided to GridView as Datasource and binds to it. Hence the output is displayed as:
This is how dataSource is binded through code to GridView…hope enjoyed it..Jazakallah khair…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s