lundi 12 février 2018

Adding Checkboxes to a GridView in Visual Studio

I am trying to add a column of checkboxes to my grid view so that I can export only selected rows to Excel(current it exports the whole grid). I am using ASP.Net and C# and I am new to both languages. When I add a the checkbox column it displays with a checkbox in each row.It works until I link the master page to the aspx page and then the column appears but only one check box appears. The checkbox also does not appear in any row but half way down the page. Does anyone know how I can fix this or another way to select multiple rows from a data grid to export(the checkbox method is preferable)? Any help greatly appreciated.

The following is my ASP.Net Code:

<%@ Page Language="C#" EnableEventValidation="false"      AutoEventWireup="true" CodeBehind="Shipment.aspx.cs"    Inherits="CertificateApplication.Shipment" MasterPageFile="~/Site.Master"%>



<asp:Content ID="BodyContent" ContentPlaceHolderID="BodyContent" runat="server">
<h1 style="color:white;">Shipment Details</h1>
<br/>
<br/>
<div class="container-fluid" ">
  <div class="row">
    <div class="col-12">
        <fieldset style="margin-right:20px;" >
            <!--Panel containing a grid view of data from the database. Not all data is selected, only the data required for the excel document-->
                <asp:Panel ID="DataGrid" runat="server" ScrollBars="Horizontal" style="z-index:-1; margin-right: 248px;" Height="800px" Width="1206px">           
                <asp:SqlDataSource ID="SqlDataSourceCertificate" runat="server" ConnectionString="<%$ ConnectionStrings:CertificateDBConnectionString1 %>" SelectCommand="SELECT * FROM [ProductDetails]"></asp:SqlDataSource>
                <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" DataKeyNames="CertificateNumber" DataSourceID="SqlDataSourceCertificate" ForeColor="#333333" GridLines="None" Height="320px" Width="1327px" Font-Size="Small" >
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:BoundField DataField="CertificateNumber" HeaderText=" Certificate No." ReadOnly="True" SortExpression="CertificateNumber" />
                    <asp:BoundField DataField="ProductDescription" HeaderText="Name of Product   " SortExpression="ProductDescription" />
                    <asp:BoundField DataField="NetWeight" HeaderText="Weight" SortExpression="NetWeight" />
                    <asp:BoundField DataField="NoPackages" HeaderText= Number of  packages" SortExpression="NoPackages" />
                    <asp:TemplateField HeaderText="Select">
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns
            </asp:GridView>
             </asp:Panel>
            <!--Buttons to export the data to excel -->
            <asp:Button ID="btnExport" runat="server" Text="Export to Excel" style=" left: 201px; top: 807px; position: absolute; width: 261px; height: 38px;" OnClick="btnExport_Click"/>
            <asp:Button ID="btnExportSelected" runat="server" Text="Export Selected Record to Excel" style=" left: 496px; top: 808px; position: absolute; width: 494px; height: 38px;" OnClick="btnExportSelected_Click"/>
        </fieldset>
      </div>
    </div>
    </div>
<!-- /.container-fluid-->
<!-- /.content-wrapper-->
<!-- Scroll to Top Button-->
<a class="scroll-to-top rounded" href="#page-top">
  <i class="fa fa-angle-up"></i>
</a>

The following is my C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;

namespace CertificateApplication
{
public partial class Shipment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }

   protected void btnExport_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition",       string.Format("attachment;filename={0}", "Certificates.xls"));
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.RenderControl(hw);
        Response.Write(sw.ToString());
        Response.End();

    }

}   

}

I can provide my Master Code if needed! Thanks




Aucun commentaire:

Enregistrer un commentaire