-->

01 February 2020

Asp.Net GridView Control

  Asp.Net CS By Example       01 February 2020


 GridView Control 

  GridView control is a data control and is more complex and offer many more features compare to other data controls (e.g.: Repeater,DataList). These additional features come at a cost—they consume more memory and processing power on the server,so they should only be used when they deliver real benefits. Otherwise, it’s best to use a simpler controls.

 The GridView control generates simple HTML tables, so information within a GridView is presented to the end user in a familiar, cleanly formatted, tabular structure.

 The GridView offers several more advanced features, such as sorting and paging (that is, splitting a large result set into pages), and makes it easy to modify the data in the underlying data source.

 The GridView control provides the following functionality:
  ■ database table-like presentation of data
  ■ table headers and footers
  ■ paging
  ■ sorting
  ■ style modification through templates
  ■ customizable columns for advanced editing


 As with any other ASP.NET server control, GridView controls are added to the page using a specific element:

<asp:GridView id="grid" runat="server" /> 

 Once we add the GridView to the page, we can bind an SqlDataReader to it as follows:

 grid.DataSource = myDataReader;
 grid.DataBind(); 

 The GridView takes the structure of the database table automatically, and presents the data to the user in a cleanly formatted HTML table.

  Code: GridView01.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView01.aspx.cs" 
Inherits="LearnAsp.Net.ControlDemo.GridView.GridView01" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>
            Employee Details</h1>
        <asp:GridView ID="grid" runat="server" />
    </div>
    </form>
</body>
</html>


  Code: GridView01.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Configuration;

namespace LearnAsp.Net.ControlDemo.GridView
{   
    public partial class GridView01 : System.Web.UI.Page
    {
        static String strConn = ConfigurationManager.ConnectionStrings["LearnAsp"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            GridBind();
        }

        protected void GridBind()
        {
            String query = " select * from emp ";
            OracleConnection objConn;
            OracleCommand objCmd;
            
            objConn = new OracleConnection(strConn);
            objCmd = new OracleCommand(query, objConn);

            DataTable dt = new DataTable();
            objConn.Open();
            OracleDataAdapter oda = new OracleDataAdapter(objCmd);
            oda.Fill(dt);
            objConn.Close();

            if (dt.Rows.Count > 0)
            {
                grid.DataSource = dt;
                grid.DataBind();
            }
            else
            {
                grid.DataSource = null;
                grid.DataBind();
            }

        }
    }
}
  Output:
logoblog

Thanks for reading Asp.Net GridView Control

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.