DataBinding a GridView control

DataBinding is done to attach data coming from a Sql server database or from a custom object to a GridView control. To bind data from the Sql server database, we use the SqlDataSource control and to bind data from custom object, we use the ObjectDataSouce control.



There are two ways of DataBinding a GridView control. One is Declarative databinding and the other is programmatic databinding. In Declarative databinding, we use DataSourceID property to bind the GridView control to a DataSource control. In Programmatic databinding, we use DataSource property and DataBind() method to bind the GridView control.

In Declarative Databinding, operations such as selecting and updating are done automatically. When we adopt Programmatic Databinding, GridView will not be able to perform these functions automatically. These functions have to be performed programmatically.

The example below shows how to declaratively databind a GridView control.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
 ConnectionString="<%$Connectionstrings:ERPConnectionString%>"
         SelectCommand="SELECT * FROM AccountsTable">
     </asp:SqlDataSource>  
        <asp:GridView ID="GridView1"  DataSourceID="SqlDataSource1"  
        AutoGenerateColumns="true" runat="server">
        </asp:GridView>