Hi,
Lets come back to ASP.NET tips and trick series. We are once again going to learn new trick related to GridView. Previously I have posted following articles related to GridView.
- Export GridView data to Excel
- Pagination in GridView
- Export GridView data to Word
- Custom Error Message for Empty GridView
- GridView with scroll bar
- GridView with Fixed Header and Scroll Bar
Requirement
Code for Default.aspx
This is the code for GridView.
[html]
<asp:GridView runat="server" ID="gvProducts" AutoGenerateColumns="false" AllowPaging="false" AlternatingRowStyle-BackColor="Linen" HeaderStyle-BackColor="SkyBlue" Width="100%" OnPageIndexChanging="gvProducts_PageIndexChanging" EmptyDataText="Sorry! No Products to List. First Add from Add Product Link.">
<Columns>
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<asp:Label ID="lblProductID" runat="server" Text='<%#Eval("ProductID")%>’ ToolTip="ID of Product as stored in Database."></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:Label ID="lblProductName" runat="server" ToolTip="Name of Product" Text='<%#Eval("ProductName")%>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Brand">
<ItemTemplate>
<asp:Label ID="lblBrandName" runat="server" ToolTip="Brand of Product" Text='<%#Eval("BrandName")%>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Category">
<ItemTemplate>
<asp:Label ID="lblProductCat" runat="server" ToolTip="Category of Product" Text='<%#Eval("CategoryName")%>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="In Stock">
<ItemTemplate>
<asp:Label ID="lblProductinStock" runat="server" ToolTip="Quantity available in Stock"
Text='<%#Eval("UnitsInStock")%>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
[/html]
Code for Default.aspx.cs
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConToStore"].ConnectionString);
SqlDataAdapter adp = new SqlDataAdapter("Select * from Products,Brands,Category where Products.BrandID=Brands.BrandID and Products.CategoryID=Category.CategoryID", con);
DataSet ds = new DataSet();
adp.Fill(ds);
gvProducts.DataSource = ds.Tables[0];
gvProducts.DataBind();
}
}
[/csharp]
Printing Page.
Printing GridView Only
Wrap the GridView inside a Table or Panel. I am using Table. You can also go with Panel.
<tr>
<td colspan="2" align="center">
<h1>All Products in Store</h1>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td colspan="2">
<asp:GridView runat="server" ID="gvProducts" AutoGenerateColumns="false" AllowPaging="false"
AlternatingRowStyle-BackColor="Linen" HeaderStyle-BackColor="SkyBlue" Width="100%"
OnPageIndexChanging="gvProducts_PageIndexChanging"
EmptyDataText="Sorry! No Products to List. First Add from Add Product Link.">
……………
………….
</asp:GridView>
</td>
</tr>
<tr>
<td align="right">
<asp:LinkButton ID="lnkPrint" runat="server" ToolTip="Click to Print All Records" Text="Print Data" OnClick="lnkPrint_Click"></asp:LinkButton>
<asp:LinkButton ID="lnkExportAll" runat="server" ToolTip="Export this List" Text="Export to Excel" OnClick="lnkExportAll_Click"></asp:LinkButton><asp:LinkButton ID="lnkAddNew" runat="server" ToolTip="Add New Product"
Text="Add New" OnClick="lnkAddNew_Click"></asp:LinkButton>
</td>
</tr>
</table>
[/html]
Now just add following JavaScript function in head section and assign that function to Print linkbutton.
[javascript] <script language="javascript" type="text/javascript">
function PrintPage() {
var printContent = document.getElementById(‘<%= pnlGridView.ClientID %>’);
var printWindow = window.open("All Records", "Print Panel", ‘left=50000,top=50000,width=0,height=0’);
printWindow.document.write(printContent.innerHTML);
printWindow.document.close();
printWindow.focus();
printWindow.print();
}
</script>
[/javascript]
Now lets click on Print Link button which we have created. Following will be output for above code.
Bingo! It was the requirement.
Hope you enjoyed reading. If you have any feedback or suggestions, please send us as comment or using contact options. Keep sharing.
John Bhatt
1 comment
I used to be recommended this website via my cousin. I
am now not positive whhether or not this submit is written by him as
nobody else realize such precise about myy trouble.
You’re incredible! Thanks!