Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Excel
Hi,
My module is to, enter values in ui and the values ll be saved in excel.. my fontend is like this,
 
CodeID: -------------
Content: ------------
Mapping Code: ------------
 
[submit][save]
 
after clicking submit button, a grid appears with the values entered and by clicking save, the values are stored in an excel.. everything is working fyn..
 
now, i want to add an edit link in the grid. if i click edit, the values should be populated in the text box and after editing and by giving save, the values should be edited and saved in excel sheet..
 
can someone please help me with codes..
below is my source and code behind:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="UItoExcelDuplicate._Default" %>
 
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
        }
        .style3
        {
            width: 148px;
        }
    </style>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
 
    <table class="style1">
        <tr>
            <td class="style3">
                <asp:Label ID="Label1" runat="server" Text="CodeID"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtCodeID" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Label ID="Label2" runat="server" Text="Content"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtContent" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Label ID="Label3" runat="server" Text="Mapping Code"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtMappingCode" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                &nbsp;</td>
            <td>
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="65px"
                    onclick="btnSubmit_Click" />
                <asp:Button ID="btnSave" runat="server" Text="Save" Width="67px"
                    onclick="btnSave_Click" />
            </td>
        </tr>
        <tr>
            <td class="style2" colspan="2">
 
           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
    ForeColor="#333333" Width="395px" AllowPaging ="True">
                <FooterStyle BackColor="#507CD1" Font-Bold ="True" ForeColor="White" />
    <RowStyle BackColor="#EFF3FB" />
    <EditRowStyle BackColor ="#2461BF" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White" />
 

    <columns>
    <asp:BoundField HeaderText="CodeID" DataField="CodeID" />
    <asp:BoundField HeaderText="Content" DataField="Content" />
    <asp:BoundField HeaderText="Mapping Code" DataField="Mapping Code" />
    <asp:CommandField ShowEditButton="true" />
    </columns>
                </asp:GridView>
                <br />
                <asp:Label ID="lblMessage" runat="server"></asp:Label>
            </td>
        </tr>
    </table>
 
</asp:Content>
Code behind:
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.OleDb;
 
namespace UItoExcelDuplicate
{
    public partial class _Default : System.Web.UI.Page
    {
        private DataTable _dt;
        public DataTable dt
        {
            get
            {
                return _dt;
            }
            set
            {
                _dt = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("CodeID", typeof(string));
                dt.Columns.Add("Content", typeof(string));
                dt.Columns.Add("Mapping Code", typeof(string));
                Session["dt"] = dt;
            }
            _dt = (DataTable) Session ["dt"];
            
        }
        private void BindGrid()
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string con = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = D:/test.xlsx;Extended Properties='Excel 12.0 Xml;HDR=YES'";
            OleDbConnection obj = new OleDbConnection(con);
            obj.Open();
            OleDbCommand cmd = new OleDbCommand("Insert into [Sheet1$]([CodeID],[Content],[Mapping Code]) values('" + txtCodeID.Text + "','" + txtContent.Text + "','" + txtMappingCode.Text + "')", obj);
            cmd.ExecuteNonQuery();
            lblMessage.Text = "Inserted";
            obj.Close();
            txtCodeID.Text = txtContent.Text = txtMappingCode.Text = string.Empty;
        }
 
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)Session["dt"];
            DataRow dr = dt.NewRow();
            dr["CodeID"] = txtCodeID.Text;
            dr["Content"] = txtContent.Text;
            dr["Mapping Code"] = txtMappingCode.Text;
 
            dt.Rows.Add(dr);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            BindGrid();
        }
 
        
    }
}
Posted 17-Sep-12 23:28pm
Edited 17-Sep-12 23:33pm
v2
Comments
vithal wadje at 18-Sep-12 4:39am
   
next time don't use shorts cuts to explain your questions..
Ashraff Ali Wahab at 18-Sep-12 11:14am
   
What I understand is your grid need to be editable.Am I correct?
Anusha Sridhar at 20-Sep-12 0:16am
   
yes. and the values should be edited in excel too.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100