Hopefully an easy one!
So currently I have a gridview which links to another page which works perfectly. However I also wish to export the gridview to CSV which also works perfectly with the exception it treats the appended select field as another column and consequently shifts the column headers to the left misaligning the data to the header and omitting the last columns data.
Is there an obvious way to create a column header for the appended select button? Alternatively cater for this within the vb behind the export button
?
Cheers, Paul.
So currently I have a gridview which links to another page which works perfectly. However I also wish to export the gridview to CSV which also works perfectly with the exception it treats the appended select field as another column and consequently shifts the column headers to the left misaligning the data to the header and omitting the last columns data.
Is there an obvious way to create a column header for the appended select button? Alternatively cater for this within the vb behind the export button
?
Code:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv")
Response.Charset = ""
Response.ContentType = "application/text"
GridView1.AllowPaging = False
GridView1.DataBind()
Dim sb As New StringBuilder()
For k As Integer = 0 To GridView1.Columns.Count - 1
'add separator
sb.Append(GridView1.Columns(k).HeaderText + ","c)
Next
'append new line
sb.Append(vbCr & vbLf)
For i As Integer = 0 To GridView1.Rows.Count - 1
For k As Integer = 0 To GridView1.Columns.Count - 1
'add separator
sb.Append(GridView1.Rows(i).Cells(k).Text + ","c).Replace(" ", String.Empty)
Next
'append new line
sb.Append(vbCr & vbLf)
Next
Response.Output.Write(sb.ToString())
Response.Flush()
Response.End()
End Sub