Click here to Skip to main content
15,036,145 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi all,

I have columns in a gridview like(For eg)

Id Firstname Lastname

123 Code Project


Now I want to print it as below without Id column.


Firstname Lastname

Code Project

To print gridview used javascript is :

C#
function CallPrint() {
    var prtContent = document.getElementById("divPrint");
   
    var WinPrint = window.open('', 'prtContent ', 'left=100,top=100,width=100,height=100,tollbar=0,scrollbars=1,status=0,resizable=1');
    datagridview.Columns[0].Visible = false;
    WinPrint.document.write(prtContent.innerHTML);
    WinPrint.document.close();
    WinPrint.focus();
    WinPrint.print();
    WinPrint.close();
}



How to achieve this by using javasript.

Thank you,
Posted
Updated 6-Mar-13 21:56pm
v4

same as in below link's Solution 1 ...but u have to do some little changes in it
How to print the gridview selected columns?[^]
C#
function CallPrint() {
    var prtContent = document.getElementById('<%= GridView1.ClientID %>');
           prtContent.border = 1; //set no border here

           var WinPrint = window.open('','','left=0,top=100,width=80,height=100,toolbar=0,scrollbars=1,status=0,resizable=1');
           WinPrint.document.write(prtContent.outerHTML);


       var rows = WinPrint.document.getElementById("GridView1").rows;
       for(var i=0;i<rows.length;i++)
       {

        rows[i].deleteCell(0);
       }

           WinPrint.document.close();
           WinPrint.focus();
           WinPrint.print();
           WinPrint.close();
}

after browsing ur page view source of page u will understand trick...
hope it will help u
   
v2
When printing the page,
first hide the unwanted columns
then call the print() function
and then show previously hide columns

Hide grid view column using javascript

http://forums.asp.net/t/1542978.aspx/1[^]
http://stackoverflow.com/questions/6068348/javascript-hide-multiple-gridview-rows[^]
http://aspdotnetcodebook.blogspot.com/2008/03/how-to-hide-gridview-column-using.html[^]
   
v2
Comments
bapu_reddy 7-Mar-13 7:11am
   
How to hide unwanted columns in java script??
Tharaka MTR 7-Mar-13 7:25am
   
I have updated the answers with some links

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900