There are two ways you can bind a GridView. You can either manually do it by setting the DataSource
property and calling DataBind()
, or you can use a DataSourceControl such as a SqlDataSource
.
Create your GridView:
<asp:GridView ID="gvColors" runat="server"></asp:GridView>
First create or retrieve the source data for the GridView. Next, assign the data to the GridView's DataSource
property. Finally, call DataBind()
.
List<string> colors = new List<string>();
colors.Add("Red");
colors.Add("Green");
colors.Add("Blue");
gvColors.DataSource = colors;
gvColors.DataBind();
Create your DataSourceControl:
<asp:SqlDataSource ID="sdsColors"
runat="server"
ConnectionString="<%$ MyConnectionString %>"
SelectCommand="SELECT Color_Name FROM Colors">
</asp:SqlDataSource>
Create your GridView and set the DataSourceID
property:
<asp:GridView ID="gvColors"
runat="server"
DataSourceID="sdsColors">
</asp:GridView>