How to use GridView in

GridView Server Control
GridView is a very important control in GridView is mainly uses for display tabular data. It also can edit, update, insert data. You can use it for various requirements. This can help you to display and format tabular data very fastest way. We can format the gridview's nearly all parts as example header, row, alternate row, pager etc. Even we can place insert, edit, delete button for each row.

In this simple example we create a default formatted GridView. We also create a SqlDataSource control populate the data source by a simple database query. We fetch data from Northwind database customer table. Finally, we populate GridView by that SqlDataSource. We set the GridView sorting, paging, auto generate columns true. So sorting, paging, columns names facility are created without much more coding.

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

<!DOCTYPE html>

<script runat="server">


<html xmlns="">
<head runat="server">
    <title>GridView example: how to use GridView in</title>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID" 
                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" 
                    SortExpression="CustomerID" />
                <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" 
                    SortExpression="CompanyName" />
                <asp:BoundField DataField="ContactName" HeaderText="ContactName" 
                    SortExpression="ContactName" />
                <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" 
                    SortExpression="ContactTitle" />
                <asp:BoundField DataField="Address" HeaderText="Address" 
                    SortExpression="Address" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                <asp:BoundField DataField="Region" HeaderText="Region" 
                    SortExpression="Region" />
                <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" 
                    SortExpression="PostalCode" />
                <asp:BoundField DataField="Country" HeaderText="Country" 
                    SortExpression="Country" />
                <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                <asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>" 
            SelectCommand="SELECT * FROM [Customers]"></asp:SqlDataSource>
The above image show the output of this example. Here you can see how easily GridView present the tabular data. Paging and columns names are hyperlinked. So, if you click them it can show next page or sort by specific column.

More examples