在网格中编辑和更新. [英] edit and update in grid..

查看:70
本文介绍了在网格中编辑和更新.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的模块是在ui中输入值,然后将值保存在excel中.我的字体如下:

编码ID:-------------
内容:------------
映射代码:------------

[提交] [保存]

单击提交"按钮后,将出现一个网格,其中包含输入的值,然后单击保存",这些值将存储在excel中.
现在,我想在网格中添加一个编辑链接.如果单击编辑",则应在文本框中填充值,并在编辑后并通过保存,应将值编辑并保存在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();
        }

        
    }
}

推荐答案

([[CodeID],[Content],[Mapping Code])values('" + txtCodeID.Text + " + txtContent.Text + " ','" + txtMappingCode.Text + ')",obj); cmd.ExecuteNonQuery(); lblMessage.Text = " ; obj.Close(); txtCodeID.Text = txtContent.Text = txtMappingCode.Text = 字符串 .Empty; } 受保护的 无效 btnSubmit_Click(对象发​​件人,EventArgs e) { DataTable dt =(DataTable)Session [" ]; DataRow dr = dt.NewRow(); dr [" ] = txtCodeID.Text; dr [" ] = txtContent.Text; dr [" ] = txtMappingCode.Text; dt.Rows.Add(dr); GridView1.DataSource = dt; GridView1.DataBind(); BindGrid(); } } }
([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(); } } }


关注此链接……对您肯定有帮助..

http://www.ezineasp.net/post/ASP-Net- Gridview-Edit-Update-Cancel-Commands.aspx [ ^ ]



http://www.dreamywebsolutions.com [ ^ ]
Follow this link...it will surely help u..

http://www.ezineasp.net/post/ASP-Net-Gridview-Edit-Update-Cancel-Commands.aspx[^]



http://www.dreamywebsolutions.com[^]


请参阅以下文章

在ASP.NET 2.0中可编辑GridView [ ^ ]

在ASP.NET中编辑单个GridView单元格 [ ^ ]

谢谢
--RA
See the below articles

Editable GridView in ASP.NET 2.0[^]

Edit Individual GridView Cells in ASP.NET[^]

Thanks
--RA


这篇关于在网格中编辑和更新.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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