how to use GridView in asp.net

GridView example: how to use GridView in asp.net
gridview is a very important control in asp.net. gridview is mainly uses for display tabular data. it also can edit update insert data. you can use it for variuos 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 deafult formatted gridview. we also create a sqldatasource control populate the datasource by a simple database query. we fetch data from northwind databse 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.
GridViewHowToUse.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GridView example: how to use GridView in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID" 
            DataSourceID="SqlDataSource1">
            <Columns>
                <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" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>" 
            SelectCommand="SELECT * FROM [Customers]"></asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>
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.


Related asp.net example