Link to home
Start Free TrialLog in
Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on

1000 pts datagrid to excel with columns readonly.

I have written the program and still i can modify the columns in excel.
Here is my aspx code:
=============
<%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="PDM.excel.WebForm3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
      <HEAD>
            <title>WebForm3</title>
            <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
            <meta content="C#" name="CODE_LANGUAGE">
            <meta content="JavaScript" name="vs_defaultClientScript">
            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
      </HEAD>
      <body text="black" bgColor="silver" MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 72px" runat="server"
                        GridLines="Vertical" AutoGenerateColumns="False" CellPadding="3" BackColor="White" BorderWidth="1px" BorderStyle="None"
                        BorderColor="#E7E7FF">
                        <SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
                        <AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
                        <ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
                        <HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
                        <FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
                        <Columns>
                              <asp:BoundColumn DataField="string_id" ReadOnly="true" HeaderText="String ID"></asp:BoundColumn>
                              <asp:BoundColumn DataField="string" ReadOnly="true" HeaderText="String"></asp:BoundColumn>
                        </Columns>
                        <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></PagerStyle>
                  </asp:datagrid><asp:dropdownlist id="Language_DropDownList" style="Z-INDEX: 102; LEFT: 296px; POSITION: absolute; TOP: 24px"
                        runat="server" AutoPostBack="True"></asp:dropdownlist><asp:label id="Label1" style="Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 24px" runat="server"
                        Width="248px" Font-Bold="True">Choose Your Language To Convert:</asp:label><asp:button id="Button1" style="Z-INDEX: 104; LEFT: 392px; POSITION: absolute; TOP: 24px" runat="server"
                        Text="Submit"></asp:button></form>
      </body>
</HTML>


and asp.cs
======
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Xml.Xsl;
using System.Xml;

namespace PDM.excel
{
      /// <summary>
      /// Summary description for WebForm3.
      /// </summary>
      public class WebForm3 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.DataGrid DataGrid1;
            public SqlConnection conDB = new SqlConnection();
      
            static public string[] LanguageStr =
            {
                  "English","French","Spanish"};
            static public string[] DropDownListStr  =
            {
                  "Language_DropDownList"};
            protected System.Web.UI.WebControls.DropDownList Language_DropDownList;
            protected System.Web.UI.WebControls.Label Label1;
            protected System.Web.UI.WebControls.Button Button1;

            static public string[][] AddStrArray =
            {
                  LanguageStr};

            private void Fill_DropDownList()
            {
                  for (int i = 0; i < DropDownListStr.Length; i++)
                  {
                        string CtrlName= DropDownListStr[i];
                        DropDownList MyList =  (DropDownList)Page.FindControl(CtrlName);
                        // Populate all the checkboxes
                        string[] ToPopulate = AddStrArray[i];
                        for (int j = 0; j < ToPopulate.Length; j++)
                        {
                              MyList.Items.Add(new ListItem(ToPopulate[j], j.ToString()));
                        }
                  }
            }

            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
                  if(!Page.IsPostBack)
                  {
                        Fill_DropDownList();

                        
                  }

            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Language_DropDownList.SelectedIndexChanged += new System.EventHandler(this.Language_DropDownList_SelectedIndexChanged);
                  this.Button1.Click += new System.EventHandler(this.Button1_Click);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            public DataSet DataToExcel = new DataSet();

            

            public void BindGrid (int index)
            {
                  conDB.ConnectionString = "data source=10.195.17.7;database=devnew;uid=bounaajak;pwd=ehsfirst;packet size=4096";
                  SqlDataAdapter da = new SqlDataAdapter("Select string_id, string from pdm_translations where language_id = 1 and string_id not in (select string_id from pdm_translations where language_id =  " + index.ToString() + ") ", conDB);
                  da.Fill(DataToExcel, "DataToExcel");
                  Session["Tabla"] = Language_DropDownList.SelectedItem.Text;
                  DataToExcel.WriteXml(Server.MapPath(Session["Tabla"] + ".xml"));
                  DataGrid1.DataSource = DataToExcel;
                  DataGrid1.DataBind();
                  // Get row count
                  int nRowCount = DataToExcel.Tables["DataToExcel"].Rows.Count;
                  // Store Row count in Session variable
                  Session["RowCount"] = nRowCount;
                  

            }
            private void Language_DropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  int getSelectedIndex = Language_DropDownList.SelectedIndex+1;
                  BindGrid(getSelectedIndex);      
                  
            }

            public void DataGridToExcel(DataGrid Export,HttpResponse Response)
            {

                  // Set the Response mime type for excel
                  Response.ContentType = "application/vnd.ms-excel";
                  // Create a String Writer
                  System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                  Response.Charset = "";
                  this.EnableViewState = false;
                  System.IO.StringWriter tw = new System.IO.StringWriter();
                  System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                  DataGrid1.RenderControl(hw);
                  Response.Write(tw.ToString());
                  Response.End();

            }
            
            private void Button1_Click(object sender, System.EventArgs e)
            {
                  DataGridToExcel (DataGrid1, Response);
            }

            

      
      }
}
Avatar of praneetha
praneetha

may be you can make the whole excel file readonly...
Avatar of mathieu_cupryk

ASKER

Well If I can make the fields all readonly from datagrid. Then by all means But I have been stuck on this problem for days.
Let me investigate a little but I think You cant modify some setting of Excel from ASP.NET

You should Insted Use Visual Studio Tools for Office. Install it and give it a try.

I will confirm later if this can be donde from asp.net

Or you can try something different.
like genereate the FIle on the Server. and then with System.IO objects change the file to readonly attributes.??

The thing is with using the Excel object I have to set impersonate=true
I am not aloud to do that for this project.:-(
why? you have to think in something different for the security? Please tell us why would  u use it.

I am still confirming my investigation
I am not aloud the company will not allow me.
I cannot use a super user or with attribute impersonate = true.
The reason for the column to be read only. Is so that some does not modify it accidently.
but when you generate an excel file It Generates ON the CLIENT MACHINE! if a second User generates It Wont see any changes from the first user.
Whats your application sceneario?
It will be generated on the client machine and the client will tranlate in the third column field the desired languange. Only one person will be doing the translation. Who I don't know. This is my situation.
ASKER CERTIFIED SOLUTION
Avatar of djhex
djhex

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I must add a header and make the datagrid invisible but have the save come up?