Showing posts with label GRID VIEW EXAMPLE. Show all posts
Showing posts with label GRID VIEW EXAMPLE. Show all posts

How to download data from Grid view to Excel Sheet

April 29, 2013 |

 Charlieform.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Charlieform.aspx.cs" Inherits="Charlieform"
EnableEventValidation="false" %>

<!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 id="Head1" runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        .style1
        {
            width: 1212px;
        }
        .style2
        {
            width: 1019px;
        }
        .style3
        {
            width: 1084px;
        }
        .style5
        {
        }
        .style6
        {
            width: 128px;
            height: 30px;
        }
        .style7
        {
            width: 1084px;
            height: 30px;
        }
        .style8
        {
            height: 30px;
        }
        .style9
        {
            width: 128px;
            height: 33px;
        }
        .style10
        {
            width: 1084px;
            height: 33px;
        }
        .style11
        {
            height: 33px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    <table class="style1">
        <tr>
            <td align="center" class="style5">
                &nbsp;</td>
            <td align="center" colspan="2">
                <asp:Label ID="Label1" runat="server" Text="Student Details"
                    Font-Size="X-Large"></asp:Label>
            </td>
        </tr>
        <tr>
            <td align="center" class="style5" colspan="3">
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style5">
               
                    &nbsp;</td>
            <td class="style2" colspan="2">
               
                    <asp:GridView ID="gvdetails" runat="server" AllowPaging="true"
                        AllowSorting="true" AutoGenerateColumns="false" DataSourceID="dsdetails"
                        Height="33px" Width="1046px">
                        <RowStyle BackColor="#EFF3FB" />
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:BoundField DataField="Stud_Name" HeaderText="Name"
                                SortExpression="Stud_Name" />
                            <asp:BoundField DataField="Stud_Year" HeaderText="Year"
                                SortExpression="Stud_Year" />
                            <asp:BoundField DataField="Stud_Address" HeaderText="Address"
                                SortExpression="Stud_Address" />
                            <asp:BoundField DataField="Stud_City" HeaderText="City"
                                SortExpression="Stud_City" />
                            <asp:BoundField DataField="Stud_State" HeaderText="State"
                                SortExpression="Stud_State" />
                            <asp:BoundField DataField="Stud_Country" HeaderText="Country"
                                SortExpression="Stud_Country" />
                            <asp:BoundField DataField="Stud_Email" HeaderText="Email"
                                SortExpression="Stud_Email" />
                            <asp:BoundField DataField="Hostel" HeaderText="Hostel"
                                SortExpression="Hostel" />
                            <asp:BoundField DataField="Stud_Phone" HeaderText="Phone"
                                SortExpression="Stud_Phone" />
                            <asp:BoundField DataField="Entry_Date" HeaderText="Entry Date"
                                SortExpression="Entry_Date" />
                            <asp:BoundField DataField="Exit_Date" HeaderText="Exit Date"
                                SortExpression="Exit_Date" />
                            <asp:BoundField DataField="Stud_Room" HeaderText="Room"
                                SortExpression="Stud_Room" />
                            <asp:BoundField DataField="F_EID" HeaderText="Parents EID"
                                SortExpression="F_EID" />
                                <asp:BoundField DataField="FPH" HeaderText="Parents PH"
                                SortExpression="FPH" />
                        </Columns>
                    </asp:GridView>
                </td>
        </tr>
        <tr>
            <td class="style6">
                </td>
            <td class="style7">
                <asp:SqlDataSource ID="dsdetails" runat="server"
                    ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\data.mdf;Integrated Security=True;User Instance=True"
                    ProviderName="System.Data.SqlClient"
                    SelectCommand="select * from info where Hostel='ct'" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
            <td class="style8">
                </td>
        </tr>
        <tr>
            <td class="style9">
                </td>
            <td class="style10">
                &nbsp;&nbsp;
                <asp:Label ID="Label2" runat="server" Text="DOWNLOAD"
                    Font-Size="X-Large"></asp:Label>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:ImageButton ID="btnExcel" runat="server" ImageUrl="~/img/index.jpg"
                    onclick="btnExcel_Click" Height="33px" Width="44px" />
            </td>
            <td class="style11">
                </td>
        </tr>
        <tr>
            <td class="style6">
                &nbsp;</td>
            <td class="style7">
                &nbsp;</td>
            <td class="style8">
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style5">
                &nbsp;</td>
            <td class="style3">
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Back"
                    Width="151px" />
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style5">
                &nbsp;</td>
            <td class="style3">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
   
</asp:Content>

    </div>
    </form>
</body>
</html>









Charlieform.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;
using System.Design;
using System.Drawing;

public partial class Charlieform : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
  
    protected void btnExcel_Click(object sender, ImageClickEventArgs e)
    {
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "StudentInfo.xls"));
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        gvdetails.AllowPaging = false;
        gvdetails.DataBind();
        //Change the Header Row back to white color
        gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
        //Applying stlye to gridview header cells
        for (int i = 0; i < gvdetails.HeaderRow.Cells.Count; i++)
        {
            gvdetails.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
        }
        int j = 1;
        //This loop is used to apply stlye to cells based on particular row
        foreach (GridViewRow gvrow in gvdetails.Rows)
        {
            gvrow.BackColor = Color.White;
            if (j <= gvdetails.Rows.Count)
            {
                if (j % 2 != 0)
                {
                    for (int k = 0; k < gvrow.Cells.Count; k++)
                    {
                        gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                    }
                }
            }
            j++;
        }
        gvdetails.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("charlie.aspx");
    }
    protected void gvdetails_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

How to display data in grid view

March 21, 2013 |

DISPLAYSTUDENT.ASPX.CS

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class DisplayUsers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            SqlConnection con = new SqlConnection(DataBase.connect);
            SqlDataAdapter da = new SqlDataAdapter("select * from Stud_Info", con);
            SqlCommandBuilder cm = new SqlCommandBuilder(da);
            DataSet ds = new DataSet();
            da.Fill(ds, "Stud_Info");
            if (ds.Tables["Stud_Info"].Rows.Count > 0)
            {
                GridView1.DataSource = ds.Tables["Stud_Info"];
                GridView1.DataBind();
            }
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      
    }
}

DISPLAYSTUDENT.ASPX

<%@ Page Language="C#" MasterPageFile="~/Hostel.master" AutoEventWireup="true" CodeFile="DisplayStudent.aspx.cs" Inherits="DisplayUsers" Title="DisplayStudent" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            height: 16px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  
    <table class="style1">
        <tr>
            <td align="center" colspan="2">
                <asp:Label ID="Label1" runat="server" Text="Student Details" Font-Size="Medium"></asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2" colspan="2">
                <asp:Panel ID="Panel1" runat="server" ScrollBars="Both" Height="471px"
                    Width="682px">
                    <asp:GridView ID="GridView1" runat="server" BackColor="White"
                        BorderColor="#CC9966" BorderStyle="None"
                        BorderWidth="1px" CellPadding="4"
                        Height="467px" Width="682px"
                           onselectedindexchanged="GridView1_SelectedIndexChanged">
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                        <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <EditRowStyle BackColor="#999999" />
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    </asp:GridView>
                </asp:Panel>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Back"
                    Width="151px" />
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
   
</asp:Content>





Student Details
 
Stud_IDStud_NameStud_YearStud_AddressStud_CityStud_StateStud_CountryStud_EmailHostelStud_PhoneStud_FeeEntry_DateExit_DateStud_RoomF_EID
500006101Koyyana Venkata SarthakFirstAzad NagarDelhiDelhiIndiakoyyana@ymail.comBravo9050383232Paid3/8/2013 12:00:00 AM3/20/2013 12:00:00 AM45ter@gmail.com
500012328Partha MehrotraSecond New Model townAssamAssamIndiapartha@gmail.comBravo9876543212Paid3/12/2013 12:00:00 AM3/21/2013 12:00:00 AM09gmail@gmail.com
500012717Kshitij DeopaThirdModeltownPanipatHaryanaIndiadeopa@gmail.comBravo9823456654Paid3/7/2013 12:00:00 AM3/18/2013 12:00:00 AM88 
500017072Sachin AgarwalSecondH.No 465 Kirti NagarDelhiDelhiIndiasachin@gmai.comAlpha9723454345Pending  13sachin@gmail.com
500543250SumitFirstPanipatpanipatharyanaSri Lankaesh@gmail.comAlpha3214509876Pending3/14/2013 12:00:00 AM 23 
500543255SumitFirstPanipatpanipatharyanaSri Lankaesh@gmail.comAlpha3214509876Pending3/14/2013 12:00:00 AM 23 
500564399YogeshSecondPanipatpanipatharyanaChinaes@gmail.comCharlie9070575432Pending3/12/2013 12:00:00 AM 45 
500959312RohitFirstDelhiDelhiDelhiIndiarohit@gmail.comDelta9896092048Paid2/25/2013 12:00:00 AM 32 
505967521AnilFirstDelhiDelhiHaryanaIndiaanil@gmail.comAlpha9896097048Pending3/13/2013 12:00:00 AM3/22/2013 12:00:00 AM10