What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 30541 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > ASP.NET > select record in grid viwe export in excel file? ...
Sagarp

select record in grid viwe export in excel file?

 Code Snippet posted by: Sagarp | Posted on: 1/25/2010 | Category: ASP.NET Codes | Views: 1266 | Status: [Member] | Alert Moderator   


Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Imports System.Threading
Imports System.IO
Imports System.Reflection
Partial Class Customerpurches
Inherits System.Web.UI.Page
Public conmgr As New connectionmanager

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


Dim Sql1 As String
Sql1 = " SELECT Product, SubProduct, Price, CASE qty WHEN 0 THEN 'No' ELSE 'Yes' END Qty FROM Productinfo"
If IsPostBack = False Then

Dim ds2 As DataSet = conmgr.selectdata(Sql1)


GridView2.DataSource = ds2.Tables("temp")
GridView2.DataBind()
'grid()


End If
End Sub







Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click


Button1.Visible = True
Dim dt As New DataTable()
dt.Columns.Add("Product")
dt.Columns.Add("SubProduct")
dt.Columns.Add("price")
For Each row As GridViewRow In GridView2.Rows
Dim chk As CheckBox = DirectCast(row.FindControl("CheckBox1"), CheckBox)
If chk.Checked = True Then
Dim i As Integer = row.RowIndex
Dim lbl As Label = DirectCast(GridView2.Rows(i).FindControl("lblProduct"), Label)
Dim lbl1 As Label = DirectCast(GridView2.Rows(i).FindControl("lblsubProduct"), Label)
Dim lbl2 As Label = DirectCast(GridView2.Rows(i).FindControl("lblprice"), Label)
Dim dr As DataRow = dt.NewRow()
dr("Product") = Convert.ToString(lbl.Text)
dr("SubProduct") = Convert.ToString(lbl1.Text)
dr("price") = Convert.ToString(lbl2.Text)
dt.Rows.Add(dr)
End If
Next
Session("me") = dt
Dim dg As New GridView
dg.DataSource = dt
dg.DataBind()
Response.Buffer = True
Response.ContentType = "application/ms-excel"
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.xls", "selectedrows"))
Response.Charset = ""
Dim stringwriter As System.IO.StringWriter = New StringWriter()
Dim htmlwriter As New HtmlTextWriter(stringwriter)
dg.RenderControl(htmlwriter)
Response.Write(stringwriter.ToString())
Response.End()



End Sub




End Sub
End Class

Thanks
SagarP
http://www.emanonsolutions.net
http://emanonsolutions.blogspot.com/
Found interesting? Add this to:


 Responses

Webmaster
Posted by: Webmaster | Posted on: 1/26/2010 | Level: HonoraryPlatinum | Status: [Administrator] | Alert Moderator 

Hi Sagar,

Can you please keep the above code snippet in the CODE block?

Currently its looking very scattered, please format it so that it looks nice.

Thank you.

Best regards,
Webmaster
http://www.dotnetfunda.com

>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 2:40:13 PM