dataGrid 多行外于编辑状态

来源:互联网 发布:林黛玉 薛宝钗 知乎 编辑:程序博客网 时间:2024/05/22 00:49

<%@ Page language="c#" Codebehind="main.aspx.cs" AutoEventWireup="false" Inherits="test1.WebForm6" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm6</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">
  <LINK href="xpTable.css" type="text/css" rel="stylesheet">
  <script>
  function Change_Style(font)
  {
  
  
  font.style.textAlign="left";
  font.style.color="#ff0033";
  font.style.fontWeight="bold";
  font.size=17;
  
  
  }
function keyDown()
{
 if(event.keyCode==13)
 {
  event.keyCode=9;
  return true;
 }
}

document.onkeydown=keyDown;

  </script>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体"><font size="6">
     <p align="center"><asp:label id="name" Font-Size="20pt" Runat="server"></asp:label>表本录入<INPUT id="csdn" style="Z-INDEX: 101; LEFT: 504px; POSITION: absolute; TOP: 24px" type="text"></p>
    </font>
    <asp:datagrid id="DataGrid1" runat="server" Font-Size="9pt" DataKeyField="ID" AutoGenerateColumns="False"
     Height="195px" Width="1854px">
     <FooterStyle Font-Size="9pt" HorizontalAlign="Left" ForeColor="Red" BackColor="Aqua"></FooterStyle>
     <AlternatingItemStyle Font-Size="9pt" HorizontalAlign="Left" BackColor="#E8F4FF"></AlternatingItemStyle>
     <Columns>
      <asp:BoundColumn DataField="ID" ReadOnly="True" HeaderText="ID"></asp:BoundColumn>
      <asp:BoundColumn DataField="CustomerName" ReadOnly="True" HeaderText="CustomerName"></asp:BoundColumn>
      <asp:BoundColumn DataField="OrderNo" ReadOnly="True" HeaderText="OrderNo"></asp:BoundColumn>
      <asp:BoundColumn DataField="GaugeId" ReadOnly="True" HeaderText="GaugeId"></asp:BoundColumn>
      <asp:TemplateColumn HeaderText="NewFread">
       <ItemTemplate>
        <asp:TextBox id="NewFread" Runat=server Text='<%#DataBinder.Eval(Container.DataItem,"NewFread")%>' CssClass="inputcss" onchange="Change_Style(this)">
        </asp:TextBox>
       </ItemTemplate>
      </asp:TemplateColumn>
      <asp:TemplateColumn HeaderText="NewTread">
       <ItemTemplate>
        <asp:TextBox id="NewTread" Runat=server Text='<%#DataBinder.Eval(Container.DataItem,"NewTread")%>' CssClass="inputcss" onchange="Change_Style(this)" >
        </asp:TextBox>
       </ItemTemplate>
      </asp:TemplateColumn>
      <asp:TemplateColumn HeaderText="NewGread">
       <ItemTemplate>
        <asp:TextBox id="NewGread" Runat=server Text='<%#DataBinder.Eval(Container.DataItem,"NewGread")%>' CssClass="inputcss" onchange="Change_Style(this)" >
        </asp:TextBox>
       </ItemTemplate>
      </asp:TemplateColumn>
      <asp:BoundColumn DataField="OldFread" ReadOnly="True" HeaderText="OldFread"></asp:BoundColumn>
      <asp:BoundColumn DataField="OldTread" ReadOnly="True" HeaderText="OldTread"></asp:BoundColumn>
      <asp:BoundColumn DataField="OldGread" ReadOnly="True" HeaderText="OldGread"></asp:BoundColumn>
      <asp:BoundColumn DataField="ShownTPower" ReadOnly="True" HeaderText="ShowTpower"></asp:BoundColumn>
      <asp:BoundColumn DataField="OutTpower" ReadOnly="True" HeaderText="OutTpower"></asp:BoundColumn>
      <asp:BoundColumn DataField="losspower" ReadOnly="True" HeaderText="losspower"></asp:BoundColumn>
      <asp:BoundColumn DataField="lineloss" ReadOnly="True" HeaderText="lineloss"></asp:BoundColumn>
      <asp:BoundColumn DataField="RealTpower" ReadOnly="True" HeaderText="RealTpower"></asp:BoundColumn>
      <asp:BoundColumn DataField="Ltfee" ReadOnly="True" HeaderText="Ltfee"></asp:BoundColumn>
      <asp:BoundColumn DataField="Powerfee" ReadOnly="True" HeaderText="Powerfee"></asp:BoundColumn>
      <asp:BoundColumn DataField="Capacityfee" ReadOnly="True" HeaderText="Capacityfee"></asp:BoundColumn>
      <asp:BoundColumn DataField="totfee" ReadOnly="True" HeaderText="totfee"></asp:BoundColumn>
      <asp:BoundColumn DataField="AccMonth" ReadOnly="True" HeaderText="AccMonth"></asp:BoundColumn>
      <asp:BoundColumn DataField="GaugeName" ReadOnly="True" HeaderText="GaugeName"></asp:BoundColumn>
      <asp:BoundColumn DataField="minus" ReadOnly="True" HeaderText="minus"></asp:BoundColumn>
      <asp:BoundColumn DataField="GaugeTypeid" ReadOnly="True" HeaderText="GaugeTypeid"></asp:BoundColumn>
      <asp:BoundColumn DataField="CustomerId" ReadOnly="True" HeaderText="CustomerId"></asp:BoundColumn>
      <asp:BoundColumn DataField="SimpleCustomer" ReadOnly="True" HeaderText="SimpleCustomer"></asp:BoundColumn>
     </Columns>
    </asp:datagrid></FONT>
   <table>
    <tr>
     <td><asp:button id="UpTable" Text="更新到表" Runat="server" CssClass="RedButtonCss"></asp:button></td>
     <td><asp:button id="UpDataBase" Width="80px" Text="更新到数据库" Runat="server" CssClass="RedButtonCss"></asp:button></td>
     <td><asp:button id="CanCle" Width="80px" Text="取消" Runat="server" CssClass="RedButtonCss"></asp:button></td>
    </tr>
   </table>
  </form>
 </body>
</HTML>

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;

namespace test1
{
 /// <summary>
 /// WebForm6 的摘要说明。
 /// </summary>
 public class WebForm6 : System.Web.UI.Page
 {
  protected System.Data.OleDb.OleDbDataAdapter oleDbData;
  protected System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
  protected System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
  protected System.Data.OleDb.OleDbConnection oleDbConnection1;
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  long id=2;
  protected System.Web.UI.WebControls.Button Button2;
  protected System.Web.UI.WebControls.Button Button3;
  protected System.Web.UI.WebControls.Button UpTable;
  protected System.Web.UI.WebControls.Button UpDataBase;
  protected System.Web.UI.WebControls.Button CanCle;
  protected System.Web.UI.WebControls.Label name;
  long accmonth=200402;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if (!this.IsPostBack)
   {    this.Cache.Remove("lion");
    
   
            //  this.name.Text=this.Request.QueryString["date"];
                id=Convert.ToInt64(this.Request.QueryString["id"]);
    accmonth=Convert.ToInt64(System.DateTime.Now.ToString("yyyyMM"));
     this.BindData(id,accmonth);
                
   }
   
  }

  private void Edit_Power()
  {
   for(int i=0 ;i<this.DataGrid1.Items.Count;i++)
   {

         DataGridItem item=this.DataGrid1.Items[i];
      string temp=((TextBox)(item.Cells[6].FindControl("NewGread"))).Text ;
      int    flag=Convert.ToInt32(temp);
                  
    if  ((flag==0)||(flag==7)||(flag==8))
    {
     ((System.Web.UI.WebControls.TextBox)(item.Cells[4].FindControl("NewFread"))).Style.Add("text-align","center");
     ((System.Web.UI.WebControls.TextBox)(item.Cells[4].FindControl("NewFread"))).Style.Add("color","red");
     ((System.Web.UI.WebControls.TextBox)(item.Cells[5].FindControl("NewTread"))).Style.Add("text-align","center");
     ((System.Web.UI.WebControls.TextBox)(item.Cells[5].FindControl("NewTread"))).Style.Add("color","red");
     ((System.Web.UI.WebControls.TextBox)(item.Cells[6].FindControl("NewGread"))).Style.Add("text-align","center");
     ((System.Web.UI.WebControls.TextBox)(item.Cells[6].FindControl("NewGread"))).Style.Add("color","red");
    }
    else
    {
                    ((System.Web.UI.WebControls.TextBox)(item.Cells[4].FindControl("NewFread"))).ReadOnly=true;
                     ((System.Web.UI.WebControls.TextBox)(item.Cells[4].FindControl("NewFread"))).TabIndex=System.Int16.MaxValue ;
      ((System.Web.UI.WebControls.TextBox)(item.Cells[5].FindControl("NewTread"))).ReadOnly=true;
      ((System.Web.UI.WebControls.TextBox)(item.Cells[4].FindControl("NewTread"))).TabIndex=System.Int16.MaxValue ;
     ((System.Web.UI.WebControls.TextBox)(item.Cells[6].FindControl("NewGread"))).Style.Add("text-align","center");
     ((System.Web.UI.WebControls.TextBox)(item.Cells[6].FindControl("NewGread"))).Style.Add("color","red");
    
 
    }
   }

  }
  public void BindData(System.Int64 bookid ,System.Int64 accmonth)
  {
   DataTable table=(DataTable)Cache["lion"];
   if(table==null)
   {   table=new DataTable("table");
       DataColumn myColumn = new DataColumn();
    myColumn.DataType = System.Type.GetType("System.Int32");
    myColumn.ColumnName="ID";
    myColumn.AutoIncrement = true;
    myColumn.AutoIncrementSeed = 1;
    myColumn.AutoIncrementStep = 1;
    table.Columns.Add(myColumn);
    this.oleDbData.SelectCommand.Parameters["@Bookid"].Value=bookid;
    this.oleDbData .SelectCommand .Parameters["@Accmonth"].Value=accmonth;
    this.oleDbData.Fill(table);
    Cache["lion"]=table;
   }
   this.DataGrid1.DataKeyField = "ID";
   this.DataGrid1.DataSource=table;
   this.DataGrid1.DataBind();
            Edit_Power();
  }
  private void add_row(DataTable dt ,int ID,string NewFread,string NewTread,string NewGread)
  {
   try
   {
    for (int i=0 ;i<dt.Rows.Count ;i++)
    {
     DataRow row=dt.Rows[i];
     if ((int)row["ID"]==ID)
     {
      row["NewFread"]=NewFread;
      row["NewGread"]=NewGread;
      row["NewTread"]=NewTread;
     
     }

    }
   }

   catch(Exception ex)
   {
    
   }

  }
  private void updata_table()
  {
   try
   {
    DataTable dt=((DataTable)(Cache["lion"]));
                string NewFread="0";
    string NewGread="0";
    string NewTread="0";
                  int ID=0;
    for (int i=0 ;i<this.DataGrid1.Items.Count;i++)
    {
      DataGridItem dgitem=this.DataGrid1.Items[i];
      ID=(int)this.DataGrid1.DataKeys[dgitem.ItemIndex];
            NewFread=((TextBox) dgitem.Cells[4].Controls[1]).Text;
                     NewTread=((TextBox) dgitem.Cells[5].Controls[1]).Text;
      NewGread=((TextBox) dgitem.Cells[6].Controls[1]).Text;
      this.add_row(dt,ID,NewFread,NewTread,NewGread);
                        
    }
    Cache["lion"]=dt;
    this.BindData(this.id,this.accmonth);
          }
   catch(Exception ex)
   {
    
   }

  }
  
  private void update_database()
  {
   this.oleDbData.Update(((DataTable)Cache["lion"]).GetChanges());
   ((DataTable)Cache["lion"]).AcceptChanges();

  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.oleDbData = new System.Data.OleDb.OleDbDataAdapter();
   this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
   this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
   this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
   this.UpTable.Click += new System.EventHandler(this.UpTable_Click);
   this.UpDataBase.Click += new System.EventHandler(this.UpDataBase_Click);
   this.CanCle.Click += new System.EventHandler(this.CanCle_Click);
   //
   // oleDbData
   //
   this.oleDbData.SelectCommand = this.oleDbSelectCommand1;
   this.oleDbData.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
                        new System.Data.Common.DataTableMapping("Table", "Web_GaugeDataInput", new System.Data.Common.DataColumnMapping[] {
                                                       new System.Data.Common.DataColumnMapping("CustomerName", "CustomerName"),
                                                       new System.Data.Common.DataColumnMapping("OrderNo", "OrderNo"),
                                                       new System.Data.Common.DataColumnMapping("GaugeId", "GaugeId"),
                                                       new System.Data.Common.DataColumnMapping("NewFread", "NewFread"),
                                                       new System.Data.Common.DataColumnMapping("NewTread", "NewTread"),
                                                       new System.Data.Common.DataColumnMapping("NewGread", "NewGread"),
                                                       new System.Data.Common.DataColumnMapping("OldFread", "OldFread"),
                                                       new System.Data.Common.DataColumnMapping("OldTread", "OldTread"),
                                                       new System.Data.Common.DataColumnMapping("OldGread", "OldGread"),
                                                       new System.Data.Common.DataColumnMapping("ShownTPower", "ShownTPower"),
                                                       new System.Data.Common.DataColumnMapping("OutTpower", "OutTpower"),
                                                       new System.Data.Common.DataColumnMapping("AdjustTpower", "AdjustTpower"),
                                                       new System.Data.Common.DataColumnMapping("losspower", "losspower"),
                                                       new System.Data.Common.DataColumnMapping("lineloss", "lineloss"),
                                                       new System.Data.Common.DataColumnMapping("RealTpower", "RealTpower"),
                                                       new System.Data.Common.DataColumnMapping("Ltfee", "Ltfee"),
                                                       new System.Data.Common.DataColumnMapping("Powerfee", "Powerfee"),
                                                       new System.Data.Common.DataColumnMapping("CapacityFee", "CapacityFee"),
                                                       new System.Data.Common.DataColumnMapping("totfee", "totfee"),
                                                       new System.Data.Common.DataColumnMapping("AccMonth", "AccMonth"),
                                                       new System.Data.Common.DataColumnMapping("GaugeName", "GaugeName"),
                                                       new System.Data.Common.DataColumnMapping("minus", "minus"),
                                                       new System.Data.Common.DataColumnMapping("GaugeTypeid", "GaugeTypeid"),
                                                       new System.Data.Common.DataColumnMapping("CustomerId", "CustomerId"),
                                                       new System.Data.Common.DataColumnMapping("SimpleCustomer", "SimpleCustomer")})});
   this.oleDbData.UpdateCommand = this.oleDbUpdateCommand1;
   //
   // oleDbSelectCommand1
   //
   this.oleDbSelectCommand1.CommandText = "[Web_GaugeDataInput]";
   this.oleDbSelectCommand1.CommandType = System.Data.CommandType.StoredProcedure;
   this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
   this.oleDbSelectCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("@RETURN_VALUE", System.Data.OleDb.OleDbType.Integer, 4, System.Data.ParameterDirection.ReturnValue, false, ((System.Byte)(0)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null));
   this.oleDbSelectCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Bookid", System.Data.OleDb.OleDbType.Integer, 4));
   this.oleDbSelectCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Accmonth", System.Data.OleDb.OleDbType.Integer, 4));
   //
   // oleDbConnection1
   //
   this.oleDbConnection1.ConnectionString = "User ID=sa;Data Source=ZH-SERVER;Password=000;Initial Catalog=lishi_yongdian;Prov" +
    "ider=SQLOLEDB.1";
   //
   // oleDbUpdateCommand1
   //
   this.oleDbUpdateCommand1.CommandText = "[Web_GaugeDataUpdate]";
   this.oleDbUpdateCommand1.CommandType = System.Data.CommandType.StoredProcedure;
   this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("@RETURN_VALUE", System.Data.OleDb.OleDbType.Integer, 4, System.Data.ParameterDirection.ReturnValue, false, ((System.Byte)(0)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null));
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("@GaugeId", System.Data.OleDb.OleDbType.Integer, 4, "GaugeId"));
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("@NewFread", System.Data.OleDb.OleDbType.VarChar, 6, "NewFread"));
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("@NewTread", System.Data.OleDb.OleDbType.VarChar, 6, "NewTread"));
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("@NewGread", System.Data.OleDb.OleDbType.VarChar, 6, "NewGread"));
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("@Accmonth", System.Data.OleDb.OleDbType.Integer, 4, "AccMonth"));
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  

  

  private void UpTable_Click(object sender, System.EventArgs e)
  {
    this.updata_table();
  }

  private void UpDataBase_Click(object sender, System.EventArgs e)
  {
   this.updata_table();
      this.update_database();
  }

  private void CanCle_Click(object sender, System.EventArgs e)
  {
       this.Cache.Remove("lion");
  }
 }
}


 

原创粉丝点击