Friday, 14 December 2012

Paging ,Join, Searching and Export Database in Gridview


Here I am designing asp.net page and define javascript for paging in a Gridview and joining query for multiple table in database and Export Record of Gridview record ....


Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <style type="text/css">
        .style1
        {
            color: #000000;
            font-size: large;
        }
        .style2
        {
            color: #000000;
        }
        .style3
        {
            text-decoration: underline;
        }
        .style4
        {
            color: #000000;
            font-family: "Book Antiqua";
        }
    </style>
    <script language="javascript" type="text/javascript">
        function pankaj() {
            if (document.getElementById("<%=TextBox1.ClientID %>").value == ""){
                alert("Please Enter Employee Date");
            document.getElementById("<%=TextBox1.ClientID %>").focus();
            return false;
        }
        }
   
    </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div class="style1">&nbsp;&nbsp;&nbsp;&nbsp; <span class="style3">Employee&nbsp; Search
    </span> </div>
<div align="center">
    <span class="style2">Enter Employee Date </span>&nbsp;
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
        Text="Search Date" BorderStyle="None" OnClientClick="return pankaj()" />
    </div>
    <br />
    <asp:GridView ID="GridView1" runat="server" Width="912px"
        ForeColor="Black" PageSize="2"
        onpageindexchanging="GridView1_PageIndexChanging" AllowPaging="True">
        <HeaderStyle BackColor="#0099FF" ForeColor="White" Wrap="False" />
        <PagerStyle Wrap="False" />
    </asp:GridView>
    <div align="right"><span class="style4">Export Record in Word</span><asp:ImageButton
            ID="btnExcel" runat="server" ImageUrl="~/WordImage.jpg"
onclick="btnExcel_Click" /></div>
<div align="right"><span class="style4">Export Record in Word</span><asp:ImageButton
            ID="ImageButton1" runat="server" ImageUrl="~/ExcelImage.jpg"
onclick="ImageButton1_Click" /></div>
</asp:Content>

Here coding page load and click event of .cs page ..
Default.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;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Web.Security;
using System.Text;
using System.Drawing;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Give Database string");
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection("Give Database string");
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter("select DR1.Date1,  DR1.Emp_ID, DR1.First_Name, DR1.Last_Name, DR2.Task_id, DR2.Description,DR2.Status,DR2.Time_Hours,DR2.Time_Minutes,DR2.Total_Time,DR2.Time_Duration from DR1  inner join DR2 ON DR1.Date1=DR2.Date1", conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    SqlDataAdapter adapter;
    protected void Button1_Click(object sender, EventArgs e)
    {
       conn = new SqlConnection("Give Database string");
       conn.Open();
       SqlDataAdapter da = new SqlDataAdapter("select DR1.Date1,  DR1.Emp_ID, DR1.First_Name, DR1.Last_Name, DR2.Task_id, DR2.Description,DR2.Status,DR2.Time_Hours,DR2.Time_Minutes,DR2.Total_Time,DR2.Time_Duration from DR1  inner join DR2 ON DR1.Date1=DR2.Date1  WHERE DR1.Date1 LIKE '%" + TextBox1.Text + "%' OR DR2.Date1 LIKE '%" + TextBox1.Text + "%'", conn);
       DataSet ds = new DataSet();
       da.Fill(ds);
       GridView1.DataSource = ds.Tables[0];
       GridView1.DataBind();
           }
    public override void VerifyRenderingInServerForm(Control control)
    {
       
    }
    protected void btnExcel_Click(object sender, ImageClickEventArgs e)
    {
        GridView1.AllowPaging = false;
       
        GridView1.DataBind();
        Response.ClearContent();
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.doc"));
        Response.Charset = "";
        
        Response.ContentType = "application/ms-word";
       
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }



    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.ClearContent();
           Response.Buffer = true;
              Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
              Response.ContentType = "application/ms-excel";
              StringWriter sw = new StringWriter();
               HtmlTextWriter htw = new HtmlTextWriter(sw);
               GridView1.AllowPaging = false;
               GridView1.DataBind();
              GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
          for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
      {
           GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
       }
         int j = 1;
          foreach (GridViewRow gvrow in GridView1.Rows)
       {
         gvrow.BackColor = Color.White;
         if (j <= GridView1.Rows.Count)
         {
        if (j % 2 != 0)
         {
        for (int k = 0; k < gvrow.Cells.Count; k++)
        {
        gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
          }
          }
          }
         j++;
         }
        GridView1.RenderControl(htw);
       Response.Write(sw.ToString());
        Response.End();
         }
         }

No comments:

Post a Comment

What is ASP.NET? Components of ASP.NET

ASP.Net Definition -It is used for creating web-based applications.ASP.Net is a web development platform provided by Microsoft. ASP.NET i...