select record in grid viwe export in excel file?

Posted by Sagarp under ASP.NET category on | Views : 2102
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")

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()
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)
End If
Session("me") = dt
Dim dg As New GridView
dg.DataSource = dt
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)

End Sub

End Sub
End Class

Comments or Responses

Posted by: Webmaster on: 1/26/2010 Level:HonoraryPlatinum | Status: [Administrator]
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.

Login to post response