Populer Topics

GridView Control

GridView control is a grid control which display data in tabular format. It means we can bind table with GridView which can be display under gridview.

Each record of table becomes row of gridview and each columns of table be comes columns of gridview control. This is most powerful rich data control widely use in Ado.Net programming.

GridView has inbuilt features edit , update, delete or insert directly into gridview.

This probably most powerful features which abstract programmer to use gridview control in Ado.Net programming.

GridView control support following property.

Property Description
AllowPaging

Allow us to set passing if table data is to long. By default value is false we can set it true.

PageSize

This property is used to set the size of the page to display a record default value of page size is 10.

AllowSorting

Allow us to sort data in gridview.

AutoGenrateColumns

Allow us to generate all the columns which are in bounded data table.

AutoGenerateEditButton

Display delete link button along with row of gridview to delete data of gridview.

AutoGenerateEditButton

Display edit link button along with row of gridview to edit data of gridview.

AutoGenerateSelectButton

Display select link button along with row of grid view to select data of gridview.

DataSource

This is runtime property which allow us to specific data source.

 

.Aspx File

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" 
Inherits="Codeplanex.WebForm2" %>

<!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>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField HeaderText="Sr. No" />
                <asp:BoundField HeaderText="Employee Name" />
                <asp:BoundField HeaderText="Mobile" />
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 

.Cs File

using System;
using System.Data.SqlClient;

namespace Codeplanex
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        SqlConnection con=new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Demo;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = "Select * from GridViewDemo";
                con.Open();
                
                GridView1.DataSource = cmd.ExecuteReader();
                GridView1.DataBind();
                con.Close();
                
            }
        }
    }
}

 

Method Description
DataBind()

Allow us to bind the data which specifies datasource. Datasource can be datasource or datatable.

DeleteRow()

Delete specified row from grid view. We need to give index integer number to delete specified row.

UpdateRow()

Update specified row from gridview. We need to give index interger number to update specified row.

 

Event Description
DataBinding

This raised when row is delete from gridview.

RowEditing

It Raise when row is editing from gridview.

RowUpdating

It raise when row updating.

SelectedIndexChange

This event change after selected index is changed in gridview control.