Next


Database Applications in ASP.NET with ADO.NET

ii) Data Controls a) GridView: Displays the data in a table format. It is similar to “DataGridView” control in windows applications.
b) DetailsView: Displays the data in a record-by-record format with each field headings and respective values. It considers each record as a page.
c) FormView: Displays the data in a record-by-record format, in user-defined format, based on the design created under “ItemTemplate” tag.
d) ListView: Displays the all the rows in user-defined format, based on the design created under “ItemTemplate” tag.
e) DataList: It is similar to “ListView” control, but it supports to “GridLines” and “RepeatColumns”.

Demo on Data Controls

using System.Data.SqlClient;
    protected void Page_Load(object sender, EventArgs e)
    {
        string cnstr = "data source=.;user id=sa;password=123;initial catalog=sample";
        SqlConnection cn = new SqlConnection(cnstr);
        string sqlstr = "select * from students";
        SqlDataAdapter adp = new SqlDataAdapter(sqlstr, cn);
        DataSet ds = new DataSet();
        DataTable dt;
        cn.Open();
        adp.Fill(ds);
        dt = ds.Tables[0];
        GridView1.DataSource = dt;
        GridView1.DataBind();
        DetailsView1.DataSource = dt;
        DetailsView1.DataBind();
        ListView1.DataSource = dt;
        ListView1.DataBind();
        FormView1.DataSource = dt;
        FormView1.DataBind();
        DataList1.DataSource = dt;
        DataList1.DataBind(); 
        cn.Close();        
    }
    protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {
        DetailsView1.PageIndex = e.NewPageIndex;
        DetailsView1.DataBind();
    }
    protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
    {
        FormView1.PageIndex = e.NewPageIndex;
        FormView1.DataBind();
    }    

Database Applications in ASP.NET using ADO.NET, Data Controls

       <asp:FormView ID="FormView1" runat="server" AllowPaging="True" 
            onpageindexchanging="FormView1_PageIndexChanging">
            <ItemTemplate>
                <font face="Tahoma">
                    <%# Eval("Name") %> has gained <%# Eval("Marks") %> marks.<br />
                </font>
           </ItemTemplate>
        </asp:FormView>
       <asp:ListView ID="ListView1" runat="server">
            <LayoutTemplate>
                <span id="itemPlaceHolder" runat="server"></span>
            </LayoutTemplate>
            <ItemTemplate>
                <font face="Tahoma">
                    <%# Eval("Name") %> has gained <%# Eval("Marks") %> marks.<br />
                </font>
            </ItemTemplate>
        </asp:ListView>
        <asp:DataList ID="DataList1" runat="server" GridLines="Both" RepeatColumns="2">
            <SeparatorStyle BorderStyle="Solid" />
            <ItemTemplate>
                <font face="Tahoma">
                    <%# Eval("Name") %> has gained <%# Eval("Marks") %> marks.<br />
                </font>
            </ItemTemplate>
        </asp:DataList>


Note: When you set the “AllowPaging” property, you should compulsory implement the event handler for “PageIndexChanging” event.
Note: For “GridView” and “DetailsView” controls, Visual Studio supports some pre-defined auto-format styles. To choose any one of them, right click on the control and choose “Auto Format” option.