GridView的一点用法

using   System;  
  using   System.Data;  
  using   System.Configuration;  
  using   System.Collections;  
  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.SqlClient;  
   
  public   partial   class   test   :   System.Web.UI.Page  
  {  
          SqlConnection   con;  
          protected   void   Page_Load(object   sender,   EventArgs   e)  
          {  
                  if   (!Page.IsPostBack)  
                  {  
                          Data_Bind();  
                  }  
   
          }  
   
          #region   数据绑定  
   
          private   void   Data_Bind()  
          {  
                  string   str   =   "server=localhost;uid=sa;pwd=;database=lsa_shifang";  
                  con   =   new   SqlConnection(str);  
                  SqlDataAdapter   DA   =   new   SqlDataAdapter("SELECT   *   FROM   TEST_MODEL",   con);  
                  DataSet   ds   =   new   DataSet();  
                  DA.Fill(ds);  
   
                  GridView1.DataSource   =   ds;  
                  GridView1.DataBind();  
                 
          }  
          #endregion  
   
          #region   翻页  
          protected   void   GridView1_PageIndexChanging(object   sender,   GridViewPageEventArgs   e)  
          {  
                  GridView1.PageIndex   =   e.NewPageIndex;  
                  Data_Bind();  
   
          }  
          #endregion  
   
          #region   编辑  
          protected   void   GridView1_RowEditing(object   sender,   GridViewEditEventArgs   e)  
          {  
                  GridView1.EditIndex   =   e.NewEditIndex;  
                  Data_Bind();  
          }  
          #endregion  
          #region   取消  
          protected   void   GridView1_RowCancelingEdit(object   sender,   GridViewCancelEditEventArgs   e)  
          {  
                  GridView1.EditIndex   =   -1;  
                  Data_Bind();  
          }  
          #endregion  
   
          #region   更新  
          protected   void   GridView1_RowUpdating(object   sender,   GridViewUpdateEventArgs   e)  
          {  
                  GridViewRow   gr   =   GridView1.Rows[e.RowIndex];  
                  string   str   =   "server=localhost;uid=sa;pwd=;database=lsa_shifang";  
                  con   =   new   SqlConnection(str);  
                  string   up   =   "update   test_model   set   ask=@ask   where   id=@id";  
                  SqlCommand   com   =   new   SqlCommand(up,   con);  
                  com.Connection.Open();  
                 
                  com.Parameters.AddWithValue("@ask",   SqlDbType.VarChar);  
                  com.Parameters["@ask"].Value   =   ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.Trim();  
                  com.Parameters.AddWithValue("@id",   SqlDbType.Int);  
                  com.Parameters["@id"].Value   =   ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.Trim();  
   
                  com.ExecuteNonQuery();  
                  com.Connection.Close();  
                  GridView1.EditIndex   =   -1;  
                  this.Page.RegisterStartupScript("","<SCRIPT>alert('更新成功!')</SCRIPT>");  
                  Data_Bind();  
   
          }  
          #endregion  
   
          #region   删除  
          protected   void   GridView1_RowDeleting(object   sender,   GridViewDeleteEventArgs   e)  
          {  
                  string   str   =   "server=localhost;uid=sa;pwd=;database=lsa_shifang";  
                  con   =   new   SqlConnection(str);  
                  string   del   =   "delete   test_model   where   id=@id";  
                  SqlCommand   com   =   new   SqlCommand(del,   con);  
                  com.Connection.Open();  
                  com.Parameters.AddWithValue("@id",   SqlDbType.Int);  
                  com.Parameters["@id"].Value   =   GridView1.Rows[e.RowIndex].Cells[7].Text;  
                  com.ExecuteNonQuery();  
                  com.Connection.Close();  
                  this.Page.RegisterStartupScript("",   "<SCRIPT>alert('删除成功!')</SCRIPT>");  
                  Data_Bind();  
          }  
          #endregion  
  }  

转载于:https://www.cnblogs.com/matter/archive/2006/11/30/577425.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值