First create table in your database as shown below
Once
table designed enter some dummy data for our sample after that write the
following code in Default.aspx page like as shown below
Now add following namespaces in codebehind
Using C# as code behind
Using VB as code behind
Column Name
|
Data Type
|
Allow Nulls
|
UserId
|
Int
(Set Identity=true)
|
No
|
UserName
|
varchar(0)
|
Yes
|
Location
|
Varchar(50)
|
Yes
|
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>Print Gridview Data in asp.net</title>
<script type="text/javascript">
function PrintGridData() {
var prtGrid = document.getElementById('<%=gvUserInfo.ClientID
%>');
prtGrid.border = 0;
var prtwin = window.open('',
'PrintGridViewData', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1,status=0,resizable=1');
prtwin.document.write(prtGrid.outerHTML);
prtwin.document.close();
prtwin.focus();
prtwin.print();
prtwin.close();
}
</script>
</head>
<body>
<form id="form1"
runat="server">
<div>
<b>Print Gridview Data</b><br /><br />
<asp:GridView ID="gvUserInfo"
runat="server"
>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White"/>
</asp:GridView>
<input type="button" id="btnPrint" value="Print" onclick="PrintGridData()" />
</div>
</form>
</body>
</html>Now add following namespaces in codebehind
Using C# as code behind
using
System;
using
System.Data;
using
System.Data.SqlClient;
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
// This method is used to bind
gridview from database
protected void
BindGridview()
{
using (SqlConnection
con = new SqlConnection("Initial Catalog=MySampleDB;Data
Source=SureshDasari;Integrated Security=true"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select
TOP 10 UserId,UserName,Location FROM UserInformation", con);
SqlDataReader dr = cmd.ExecuteReader();
gvUserInfo.DataSource = dr;
gvUserInfo.DataBind();
con.Close();
}
}Using VB as code behind
Imports
System.Data
Imports
System.Data.SqlClient
Partial
Class VBCode
Inherits
System.Web.UI.Page
Protected
Sub Page_Load(ByVal
sender As Object,
ByVal e As
EventArgs) Handles Me.Load
If
Not IsPostBack Then
BindGridview()
End
If
End
Sub
' This method is used to bind gridview from database
Protected
Sub BindGridview()
Dim
con As New
SqlConnection("Data
Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim
cmd As New
SqlCommand("select UserName,LastName,Location
from UserInformation", con)
Dim
da As New
SqlDataAdapter(cmd)
Dim
ds As New
DataSet()
da.Fill(ds)
gvUserInfo.DataSource
= ds
gvUserInfo.DataBind()
End
Sub
End
Class
No comments:
Post a Comment