locked
Trouble exporting GridView with CheckBoxes RRS feed

  • Question

  • User615288127 posted

    I'm using Visual Studio 2015 with SQL Server 2014, VB.

    I have a GridView where the first two columns are checkboxes.  I'm using the routine below to export the GridView to Excel.  The issue I'm having is how to put an "X" in the first two columns whenever the checkbox is checked.  As of now the first two columns are empty when exported.  My columns are named as follows:

    PDF, SLDPRT, PCB Footprint, Description, Height, Comments;  the first two columns are templates, all others are bound datafields.

    VB:

            If (E1 = True Or E2 = True) And (E3 = False) Then
                Response.ClearContent()
                Response.Buffer = True
                Response.AddHeader("content-disposition", String.Format("inline; filename={0}Cutouts_and_Holes.xls", Request.QueryString("Data")))
                If E1 = True Then Response.ContentType = "application/vnd.ms-excel"
                If E2 = True And E1 = False Then Response.ContentType = "application/vnd.scalc.exe -o %1"
                Dim stringWriter As New StringWriter()
                Dim htmlTextWriter As New HtmlTextWriter(stringWriter)
                GridView1.DataBind()
                GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF")    'white; middle blue #3399FF
                For index As Integer = 0 To GridView1.HeaderRow.Cells.Count - 1
                    GridView1.HeaderRow.Cells(index).Style.Add("background-color", "#969696")   'orange, was #d17250
                Next
                Dim index2 As Integer = 1
                GridView1.RenderControl(htmlTextWriter)
                Response.Write(stringWriter.ToString())
                Response.[End]()
            End If

    ASP:

    <asp:GridView ID="GridView1" runat="server" 
            AutoGenerateColumns="False" CellPadding="4" DataKeyNames="PCB Footprint" 
            DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" 
            PageSize="25">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:TemplateField HeaderText="PDF">
                    <ItemTemplate>
                        <asp:CheckBox ID="PDF" runat="server" AutoPostBack="True" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="SLDPRT">
                    <ItemTemplate>
                        <asp:CheckBox ID="SLDPRT" runat="server" AutoPostBack="True" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="PCB Footprint" HeaderText="PCB Footprint" 
                    ReadOnly="True" SortExpression="PCB Footprint" />
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" />
                <asp:BoundField DataField="Height" HeaderText="Height" 
                    SortExpression="Height" />
                <asp:BoundField DataField="Comments" HeaderText="Comments" 
                    SortExpression="Comments" />
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>

    The basic question is: does anybody know how I can get an "X" to appear in the exported file where either of the first two column checkboxes are checked?

    Thanks!

    Thursday, February 9, 2017 12:24 AM

Answers