DataGridView控件内实现修改与删除

一转眼好长时间没与大家讨论技术了在这说句对不起,本次带来了一个在外面常见的面试题目.DataGrid(GridView)中进行编辑(修改),删除功能,一道有趣的题目.当然这次少不了  DataGrid控件使用它里面的属性生成器->按钮列中的编辑-更新-修改模板和里面的删除模板(下我会做GridView与DataGrid比较学习)

接下来就是代码的演示与讲解如有什么不懂大家在来与我沟通

 




ContractedBlock.gif ExpandedBlockStart.gif Code
  1 public partial class _Default : System.Web.UI.Page 
  2 {
  3     readonly string _connectionString = "server=.; uid=sa; pwd=sa; database=Test";//readonly是与Const一样的一个常量使用修饰符,但是是性能比Const要高
  4 
  5     protected void Page_Load(object sender, EventArgs e)
  6     {
  7         if (!this.IsPostBack)
  8         {
  9             this.DataBindDataGrid(this.dgDataValues);
 10         }
 11     }
 12     /// <summary>
 13     /// 此方法用于数据绑定控件。我在这里使用DataGrid以后会使用GridView
 14     /// </summary>
 15     private void DataBindDataGrid(DataGrid _mydgDataValues)
 16     {
 17         string _sqlString = "Select UserID,UserName,Names,Birthday From UserInfo";
 18         SqlConnection _conn = new SqlConnection(_connectionString);
 19         try
 20         {
 21             if (_conn.State == ConnectionState.Closed)
 22                 _conn.Open();
 23             SqlDataAdapter _ad = new SqlDataAdapter(_sqlString, _conn);
 24             DataSet _ds = new DataSet();
 25             _ad.Fill(_ds);
 26             _mydgDataValues.DataSource = _ds.Tables[0];
 27             _mydgDataValues.DataBind();
 28         }
 29         catch (Exception ex)
 30         {
 31             ex.ToString();
 32         }
 33     }
 34     /// <summary>
 35     /// 此事件用于编辑数据
 36     /// </summary>
 37     /// <param name="source"></param>
 38     /// <param name="e"></param>
 39     protected void dgDataValues_EditCommand(object source, DataGridCommandEventArgs e)
 40     {
 41         this.dgDataValues.EditItemIndex = e.Item.ItemIndex;//本行代码就是从DataGrid中获得e.Item.ItemIndex行号
 42         this.DataBindDataGrid(this.dgDataValues);//当点击了本行数据时候需要从数据库里面调出值然后进行编辑
 43     }
 44     /// <summary>
 45     /// 此事件用于取消。
 46     /// </summary>
 47     /// <param name="source"></param>
 48     /// <param name="e"></param>
 49     protected void dgDataValues_CancelCommand(object source, DataGridCommandEventArgs e)
 50     {
 51         this.dgDataValues.EditItemIndex = -1;//-1代码它不需要在编辑,取消编辑框
 52         this.DataBindDataGrid(this.dgDataValues);//绑定数据库值
 53     }
 54     /// <summary>
 55     /// 此事件用于修改数据
 56     /// </summary>
 57     /// <param name="source"></param>
 58     /// <param name="e"></param>
 59     protected void dgDataValues_UpdateCommand(object source, DataGridCommandEventArgs e)
 60     {
 61         string _userID = ((TextBox)e.Item.Cells[0].Controls[0]).Text;
 62         string _userName = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
 63         string _names = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
 64         string _strbirthday = Convert.ToDateTime(((TextBox)e.Item.Cells[3].Controls[0]).Text).ToShortDateString();
 65         //以上四行是取DataGrid控件行中的四行值
 66         
 67         string _sqlString = "Update UserInfo set UserName = '" + _userName + "', Names='" + _names + "',Birthday='" + Convert.ToDateTime(_strbirthday) + "' where UserID=" + Convert.ToInt32(_userID) + "";
 68         this.dgDataValues.EditItemIndex = -1;//当修改数据成功后需要取消取消编辑框
 69         
 70         this.ExecuteQuest(_sqlString);//修改数据的一个方法,带入SQL语句
 71     }
 72     /// <summary>
 73     /// 此方法用于数据增,删,改操作
 74     /// </summary>
 75     /// <param name="_strSql"></param>
 76     private void ExecuteQuest(string _strSql)
 77     {
 78         //以下代码在原来的课程中我已经讲解过
 79         SqlConnection _conn = new SqlConnection(_connectionString);//连接
 80         _conn.Open();//打开的时候需要判断和使用Try Catch,在此就不讲解了
 81         SqlCommand _cmd = new SqlCommand(_strSql, _conn);
 82         int _intExectueNotQuest = _cmd.ExecuteNonQuery();
 83         if (_intExectueNotQuest != 0)
 84         {
 85             this.DataBindDataGrid(this.dgDataValues);
 86         }
 87     }
 88     /// <summary>
 89     /// 此事件用于数据删除,在此不要使用模板列,如使用模板列使用(Button),编辑列就会直接影响到模板列,
 90     /// </summary>
 91     /// <param name="source"></param>
 92     /// <param name="e"></param>
 93     protected void dgDataValues_DeleteCommand(object source, DataGridCommandEventArgs e)
 94     {
 95         string _strUserID = e.Item.Cells[0].Text;//获得DataGrid 中UserID 
 96         string _sqlString = "Delete From UserInfo Where UserID=" + Convert.ToInt32(_strUserID) + "";//删除语句(SQL)
 97         
 98         this.ExecuteQuest(_sqlString);//修改数据的一个方法,带入SQL语句
 99     }
100 }

 

转载于:https://www.cnblogs.com/XiaoLongZhang/archive/2009/07/31/1535791.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值