AutoGenerateSelectButton with a column header

Soldato
Joined
8 Mar 2005
Posts
3,676
Location
London, UK
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
?
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
Cheers, Paul.
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
Could you just change this line:
Code:
For k As Integer = 0 To GridView1.Columns.Count - 1
To skip over the column containing the select button?
Either:
0 To GridView1.Columns.Count - 2 (if the select button is in the last column)
or:
1 To GridView1.Columns.Count - 1 (if the select button is in the first column)
 
Soldato
OP
Joined
8 Mar 2005
Posts
3,676
Location
London, UK
Code:
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 = 1 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 = 1 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

Setting both to 1 shifts the header column to the left and the last data column is still omitted.

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 = 1 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

This sets the header column correctly and shifts the data left so it's aligned, but it omits the last column data (header is there). It's as if I need to tell it to read an additional data column since I've skipped the first.

Worked it out;second for K loop.
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 = 1 To GridView1.Columns.Count - 0
                '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
 
Last edited:
Back
Top Bottom