如何从gridview传递文本框中的数据并将其保存 [英] how to pass data in textbox from gridview and save it

查看:58
本文介绍了如何从gridview传递文本框中的数据并将其保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想通过UI在Excel中动态插入值.因此,我创建了2个文本框和按钮以及一个gridview.在文本框中输入的值将在gridview中传递,并使用ExportToExcel函数将gridview传递给excel.代码如下.

源代码:

Hi,
I want to insert values in excel dynamically from UI.. So, I created 2 textboxes and button and a gridview. the value entered in the textbox is passed in the gridview and the gridview is passed to excel using ExportToExcel function.. code is given below.

source code:

<%@ Page Title="Home Page" Language="C#"  AutoEventWireup="true"

    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<head>
    <style type="text/css">
        .style1
        {
            width: 45%;
        }
    </style>
</head>


<form id="form1" runat="server">
<table class="style1">
    <tr>
        <td>
             CodeID</td>
        <td>
            <asp:TextBox ID="txtCodeID" runat="server"></asp:TextBox>
    </tr>
    <tr>
        <td>
           Content</td>
        <td>
            <asp:TextBox ID="txtContent" runat="server"></asp:TextBox>
    </tr>
    <tr>
        <td>
           MappingCode</td>
        <td>
            <asp:TextBox ID="txtMappingCode" runat="server"></asp:TextBox>
    </tr>
    <tr>
        <td>
            &nbsp;</td>
        <td>
            <asp:Button ID="btnSubmit" runat="server"

                    Text="Submit" onclick="btnSubmit_Click" />
    <asp:Button runat="server" ID="Btn_Export" Text="Export" OnClick="Btn_Export_Click" />
    </tr>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"

    ForeColor="#333333">
<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="MappingCode" DataField="MappingCode" />
    <asp:ButtonField Text="Edit" />
    <asp:ButtonField Text="Delete" />

    </columns>
</asp:GridView>
</form>



背后的代码:



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.Reflection;

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)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("CodeID", typeof(string));
        dt.Columns.Add("Content", typeof(string));
        dt.Columns.Add("MappingCode", typeof(string));
        Session["dt"] = dt;
    }
    private void ExporttoExcel(DataTable table)
    {

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        //HttpContext.Current.Response.ContentType = "application/ms-word";
        HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");
        // HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.doc");
        HttpContext.Current.Response.Charset = "utf-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
        HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
        HttpContext.Current.Response.Write("<BR><BR><BR>");
        HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
        int columnscount = GridView1.Columns.Count;

        for (int j = 0; j < columnscount; j++)

        {

            HttpContext.Current.Response.Write("<Td>");
            HttpContext.Current.Response.Write("<B>");
            HttpContext.Current.Response.Write(GridView1.Columns[j].HeaderText.ToString());
            HttpContext.Current.Response.Write("</B>");
            HttpContext.Current.Response.Write("</Td>");
        }
        HttpContext.Current.Response.Write("</TR>");
        foreach (DataRow row in table.Rows)
        {
            HttpContext.Current.Response.Write("<TR>");
            for (int i = 0; i < table.Columns.Count; i++)

            {

                HttpContext.Current.Response.Write("<Td>");
                HttpContext.Current.Response.Write(row[i].ToString());
                HttpContext.Current.Response.Write("</Td>");
            }

            HttpContext.Current.Response.Write("</TR>");
        }
        HttpContext.Current.Response.Write("</Table>");
        HttpContext.Current.Response.Write("</font>");
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }
    protected void Btn_Export_Click(object sender, EventArgs e)
    {
        ExporttoExcel(dt);
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)Session["dt"];
        DataRow drToGrid = dt.NewRow();
        drToGrid["CodeID"] = txtCodeID.Text;
        drToGrid["Content"] = txtContent.Text;
        drToGrid["MappingCode"] = txtMappingCode.Text;

        dt.Rows.Add(drToGrid);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }


}





单击exportbutton时出现运行时错误.

*我想将所有值保存在网格中,并希望将它们同时保存在excel表中.
非常感谢!

Anusha.





i am getting a runtime error when i click exportbutton.. What i want is,

* I want to save all the values in the grid and I want that to be saved in the excel sheet simultaneously.. Please help mehow to do that..

Thanks a lot!!

Anusha.

推荐答案

尝试一下
http://www.aspsnippets. com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx
Try this
http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx


Response.Clear();
Response.AddHeader("content-disposition","attachment; filename = Excel1.xls");
Response.Charset =";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType ="application/vnd.xls";
System.IO.StringWriter stringWrite =新的System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =新的HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Excel1.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();


这篇关于如何从gridview传递文本框中的数据并将其保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆