Import Excel Sheet Data To GridView on Page_Loading GridView Binding Using Asp.Net C#

Import Excel Sheet Data To GridView Page_Loading

Import Excel Sheet Data To Gridview Display Source From Solution Explorer Folder  Page_Loading Time Using in Asp.Net C#.

                      Download Coding  



                    HTML CODING

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="300px">
                <asp:TemplateField HeaderText="SELECT">
                        <asp:CheckBox ID="chkSelect" runat="server" />
                <asp:TemplateField HeaderText="COUNTRY">
                        <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("COUNTRY") %>'></asp:Label>
                <asp:TemplateField HeaderText="CITY">
                        <asp:Label ID="lblCity" runat="server" Text='<%# Eval("CITY") %>'></asp:Label>
             <HeaderStyle BackColor="#FF3300" />
            <RowStyle BackColor="Silver" />
            <AlternatingRowStyle BackColor="#9999FF" />

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Import To Labels" />
        <asp:Label ID="lblCountry" runat="server" ForeColor="#CC3300"></asp:Label>
        <asp:Label ID="lblCity" runat="server" ForeColor="#CC3300"></asp:Label>

            C# CODING

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.Data.SqlClient;
using System.Data.OleDb;

public partial class Default5 : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)

    public void BindGridView()

        string filePath = Server.MapPath("file/1.xlsx");
        string conn = System.Configuration.ConfigurationManager.ConnectionStrings["dbconExcel07"].ConnectionString;

        conn = String.Format(conn, filePath, "yes");
        OleDbConnection con = new OleDbConnection(conn);
        OleDbCommand ExcelCommand = new OleDbCommand();
        ExcelCommand.Connection = con;

        DataTable ExcelDataSet = new DataTable();
        ExcelDataSet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        DataTable dt = new DataTable();

        string ExcelSheetName = ExcelDataSet.Rows[0]["table_name"].ToString();
        ExcelCommand.CommandText = "SELECT * From [" + ExcelSheetName + "]";
        OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
        ExcelAdapter.SelectCommand = ExcelCommand;

        GridView1.DataSource = dt;

    protected void Button1_Click(object sender, EventArgs e)
        lblCountry.Text = "";
        lblCity.Text = "";

        foreach (GridViewRow gvRow in GridView1.Rows)
            CheckBox checkbox = (CheckBox)gvRow.Cells[0].FindControl("chkSelect");
            if (checkbox.Checked)
                Label lblCnt = (Label)gvRow.FindControl("lblCountry");
                Label lblCty = (Label)gvRow.FindControl("lblCity");
                lblCountry.Text += lblCnt.Text + ",";
                lblCity.Text += lblCty.Text + ",";





                        // Xls Format

              <add name ="dbconExcel03" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties='Excel 8.0;HDR={1}'"/>
              // Xlsx Format

              <add name ="dbconExcel07" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties='Excel 12.0;HDR={1}'"/>


First - Add - New Form  - Select - Web.Config file - Add MsAccess Connection String

Next - Add Gridview From  Data ToolBox - Excel Sheet Addd To solution Explorer Folder

This Is The Excel File

Next - Add NameSpaces - Call To The Page Load 

Next - Add The Xlsx - Format Excel Sheet Add file Folder

Next - Run [F5] Show Excel Sheet Data To GridView Same

Next - Bind After Show  Selected Values  -  Select- Edit Column - Add Template Fields - Change Name To Header Text

Next - Change Another Field Name Header -OK

Next - Only Assign Header Text 

Next - Select - Gridview Edit Template - Select Column 

Next - Add - CheckBox -  Change Id Name

Next - Add Label To Another Column - Change Id - Bind The Excel Sheet Header Column Name

Next - Add Label To Another Column - Change Id - Bind The Excel Sheet Header Column Name

Next - DataBound To GridView

Next - Add The Button & Two Label For Display Selected Row Values Dispaly.

Next - Select Multiple Gridview Values Display  To Label 

Next - Run [F5] The Program - Display Excel Sheet Data  & Select Checkbox Row Values Display Labels


Post a Comment