How to populate a GridView from SqlDataSource in asp.net

Populate a GridView from SqlDataSource
GridViewExample.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GridView example: how to populate GridView from SqlDataSource in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AppConnectionString %>" SelectCommand="SELECT * FROM [CUSTOMERS]" DataSourceMode="DataSet">
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="true" PageSize="5" AllowSorting="true" AutoGenerateColumns="false" AutoGenerateSelectButton="true" EmptyDataText="No Value">
            <Columns>
                <asp:BoundField DataField="CustomerID" HeaderText="Customer ID" SortExpression="CustomerID" />
                <asp:BoundField DataField="CompanyName" HeaderText="Company" SortExpression="CompanyName" />
                <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
            </Columns>
            <SelectedRowStyle BackColor="Beige" ForeColor="Red" />
            <AlternatingRowStyle BackColor="Aqua"/>
            <PagerSettings FirstPageText="Home" LastPageText="Last" Mode="NumericFirstLast" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>
Related asp.net examples