Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, March 04, 2013

Live Task Print data from GridView using JQuery




Explanation:-
  1. Keep your GridView in one Div or TD.
  2. Give some ID for that Div or TD.
  3. For using JQuery Download the latest plugin & use it.
  4. Place the GridView in the Div or TD which you had given ID and follow the code in ascx file.
Ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GridViewContentPrintUserControl.ascx.cs" Inherits="GridViewContentPrint.GridViewContentPrint.GridViewContentPrintUserControl" %>
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script language="javascript" type="text/javascript">
    function printGridViewData()
    {
        var prtContent = $("#divPrint");
        var WinPrint = window.open('', '', 'letf=0,top=0,width=100,height=100,toolbar=0,scrollbars=0,status=0');
        WinPrint.document.write(prtContent.html());
        WinPrint.document.close();
        WinPrint.focus();
        WinPrint.print();
        WinPrint.close();
    }
</script>
<div style="text-align:center;"><input id="btnPrint" type="button" value="Print" onclick="Javascript:printGridViewData();" /></div>
<div id="divPrint">
<asp:GridView ID="dgvTasksPrint" runat="server" AutoGenerateColumns="False"
        Width="100%" CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
        GridLines="Both">
    <AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="Title" DataField="Title" />
<asp:BoundField HeaderText="Status" DataField="Status" />
<asp:BoundField HeaderText="Priority" DataField="Priority" />
<asp:BoundField HeaderText="Start Date" DataField="StartDate" DataFormatString="{0:G}" />
<asp:BoundField HeaderText="End Date" DataField="DueDate" DataFormatString="{0:G}" />
</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" />
</asp:GridView>
</div>
Ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;

namespace GridViewContentPrint.GridViewContentPrint
{
    public partial class GridViewContentPrintUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    getData();
                }
            }
            catch (Exception Ex)
            {
                Response.Write(Ex.ToString());
            }
        }

        public void getData()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPListItemCollection myColl = SPContext.Current.Web.Lists["Tasks"].Items;
                if (myColl.Count > 0)
                {
                    dgvTasksPrint.DataSource = myColl.GetDataTable();
                    dgvTasksPrint.DataBind();
                }
            });
        }
    }
}

No comments:

Post a Comment