UltraWebGrid增删改(c#)

来源:互联网 发布:诸神精灵进阶数据 编辑:程序博客网 时间:2024/05/03 18:10
using   System;
using   System.Data;
using   System.Configuration;
using   System.Web;
using   System.Web.Security;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.WebControls.WebParts;
using   System.Web.UI.HtmlControls;
using   System.Data;
using   Infragistics.WebUI.UltraWebGrid;
using   System.Data.SqlClient;

public   partial   class   _Default   :   System.Web.UI.Page  
{
        private   DataSet   ds   =   new   DataSet();
        private   SqlCommand   SelectCommand1;
        private   SqlCommand   InsertCommand1;
        private   SqlCommand   UpdateCommand1;
        private   SqlCommand   DeleteCommand1;
        private   SqlConnection   con;
        private   SqlDataAdapter   da;

        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                this.GetData();
             
                if   (!this.IsPostBack)
                        this.UltraWebGrid1.DataBind();
        }

        private   void   GetData()
        {
                con   =   new   SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[ "RegPwdConnectionString "].ConnectionString);
                this.SelectCommand1   =   new   SqlCommand();
                this.SelectCommand1.CommandText   =   "SELECT   *   FROM   mjn_vn_Users ";
                this.SelectCommand1.Connection   =   con;
                this.da   =   new   SqlDataAdapter();
                this.da.SelectCommand   =   this.SelectCommand1;
                da.Fill(ds, "users ");
                ds.Tables[0].PrimaryKey   =   new   DataColumn[]   {   ds.Tables[0].Columns[ "CustomerID "]   };
                 
        }
        protected   void   UltraWebGrid1_DataBinding(object   sender,   EventArgs   e)
        {
                this.UltraWebGrid1.DataSource   =   ds.Tables[ "users "];
        }
        protected   void   UltraWebGrid1_InitializeLayout(object   sender,   Infragistics.WebUI.UltraWebGrid.LayoutEventArgs   e)
        {
                e.Layout.AllowUpdateDefault   =   AllowUpdate.Yes;
                e.Layout.AllowAddNewDefault   =   AllowAddNew.Yes;
                e.Layout.AddNewBox.Hidden   =   false;
                e.Layout.HeaderStyleDefault.HorizontalAlign   =   HorizontalAlign.Center;
                e.Layout.Bands[0].DataKeyField   =   "CustomerID ";
                this.UltraWebGrid1.Bands[0].Columns.FromKey( "CustomerID ").Hidden   =   true;
                this.UltraWebGrid1.Bands[0].Columns.FromKey( "CustomerNo ").Header.Caption= "用户编号 ";
                this.UltraWebGrid1.Bands[0].Columns.FromKey( "CustomerPwd ").Header.Caption   =   "用户密码 ";
                this.UltraWebGrid1.Bands[0].Columns.FromKey( "Email ").Header.Caption   =   "用户邮箱 ";
                this.UltraWebGrid1.Bands[0].Columns.FromKey( "Status ").Header.Caption   =   "状态 ";
                this.UltraWebGrid1.Bands[0].Columns.FromKey( "Status ").CellStyle.HorizontalAlign   =   HorizontalAlign.Center;
                this.UltraWebGrid1.Bands[0].Columns.FromKey( "Status ").Width   =   40;
                this.UltraWebGrid1.Bands[0].Columns.FromKey( "CreateDate ").Header.Caption   =   "创建时间 ";
               
        }
        protected   void   UltraWebGrid1_UpdateRowBatch(object   sender,   Infragistics.WebUI.UltraWebGrid.RowEventArgs   e)
        {
             
                UltraGridRow   oldRow   =   (UltraGridRow)e.Data;
                string   dataKey   =   e.Row.DataKey.ToString();
                switch   (e.Row.DataChanged)
                {
                        case   (DataChanged.Added):
                                DataRow   newDataTableRow   =   ds.Tables[ "Customers "].NewRow();
                                foreach   (UltraGridCell   c   in   e.Row.Cells)
                                {
                                        newDataTableRow[c.Column.Key]   =   c.Value;
                                        Response.Write(c.Value.ToString()+ " <br> ");
                                }
                                ds.Tables[0].Rows.Add(newDataTableRow);
                                e.Row.DataKey   =   e.Row.Cells.FromKey( "CustomerID ").Value;
                                break;
                        case   (DataChanged.Modified):
                                if   (dataKey!=null)
                                {
                                        DataRow   dr   =   ds.Tables[0].Rows.Find(new   object[]   {   dataKey   });
                                        if   (dr   !=   null)
                                        {
                                                foreach   (UltraGridCell   c   in   e.Row.Cells)
                                                {
                                                        if   (!dr[c.Column.Key].Equals(c.Value))
                                                        {
                                                                dr[c.Column.Key]   =   c.Value;
                                                                Response.Write(dr[c.Column.Key].ToString()+ " <br> ");
                                                        }
                                                }
                                        }
                                        e.Row.DataKey   =   e.Row.Cells.FromKey( "CustomerID ").Value;
                                }
                                break;
                }
        }
        protected   void   Button1_Click(object   sender,   EventArgs   e)
        {

                con   =   new   SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[ "RegPwdConnectionString "].ConnectionString);
                this.InsertCommand1   =   new   SqlCommand();
                this.InsertCommand1.CommandText   =   "Insert   into   mjn_vn_Users(CustomerNo,CustomerPwd,Email)   Values(?,?,?) ";
                this.InsertCommand1.Connection   =   con;

                this.InsertCommand1.Parameters.Add(new   System.Data.SqlClient.SqlParameter( "CustomerNo ",   System.Data.SqlDbType.NVarChar,   50,   "CustomerNo "));
                this.InsertCommand1.Parameters.Add(new   System.Data.SqlClient.SqlParameter( "CustomerPwd ",   System.Data.SqlDbType.NVarChar,   50,   "CustomerPwd "));
                this.InsertCommand1.Parameters.Add(new   System.Data.SqlClient.SqlParameter( "Email ",   System.Data.SqlDbType.NVarChar,   50,   "Email "));

                this.UpdateCommand1   =   new   SqlCommand();
                this.UpdateCommand1.CommandText   =   "Update   mjn_vn_Users   SET   CustomerNo=?,CustomerPwd=?,Email=?   where   CustomerID=   ? ";
                this.UpdateCommand1.Connection   =   con;

                this.UpdateCommand1.Parameters.Add(new   System.Data.SqlClient.SqlParameter( "CustomerNo ",   System.Data.SqlDbType.NVarChar,   50,   "CustomerNo "));
                this.UpdateCommand1.Parameters.Add(new   System.Data.SqlClient.SqlParameter( "CustomerPwd ",   System.Data.SqlDbType.NVarChar,   50,   "CustomerPwd "));
                this.UpdateCommand1.Parameters.Add(new   System.Data.SqlClient.SqlParameter( "Email ",   System.Data.SqlDbType.NVarChar,   50,   "Email "));
                this.UpdateCommand1.Parameters.Add(new   System.Data.SqlClient.SqlParameter( "CustomerID ",   System.Data.SqlDbType.BigInt,   8,   "CustomerID "));

                this.da.InsertCommand   =   this.InsertCommand1;

                this.da.TableMappings.AddRange
                (
                        new   System.Data.Common.DataTableMapping[]  
                        {
                                new   System.Data.Common.DataTableMapping
                                ( "Table ",   "Customers ",  
                                      new   System.Data.Common.DataColumnMapping[]  
                                      {
                                                  new   System.Data.Common.DataColumnMapping( "CustomerID ",   "CustomerID "),
                                                  new   System.Data.Common.DataColumnMapping( "CustomerNo ",   "CustomerNo "),
                                                  new   System.Data.Common.DataColumnMapping( "CustomerPwd ",   "CustomerPwd "),
                                                  new   System.Data.Common.DataColumnMapping( "Email ",   "Email ")
                                      }
                                )
                          }
                );

                this.da.UpdateCommand   =   this.UpdateCommand1;
        }
        protected   void   UltraWebGrid1_UpdateGrid(object   sender,   UpdateEventArgs   e)
        {
                if   (ds.HasChanges())
                {
                        try
                        {
                           
                                da.Update(ds.Tables[ "Table "]);
                        }
                        catch   (Exception   ex)
                        {
                                Response.Write( "出错!! "+ex.Message);
                        }
                }
       
        }
}