Chinaunix首页 | 论坛 | 博客
  • 博客访问: 512687
  • 博文数量: 88
  • 博客积分: 2256
  • 博客等级: 大尉
  • 技术积分: 921
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-08 23:20
个人简介

积硅步,行千里

文章分类

全部博文(88)

文章存档

2019年(5)

2018年(1)

2016年(15)

2015年(23)

2013年(3)

2012年(6)

2011年(3)

2010年(22)

2009年(10)

我的朋友

分类: C#/.net

2015-10-29 13:16:53

在DGV中更新行记录数据:效果如下图:

点击(此处)折叠或打开

  1. #region 在DataGridView中更新相关行数据
  2.         private void ButtonUpdate_Click(object sender, EventArgs e)
  3.         {
  4.             string dbconn = configxml.xmlconn.readxml(); //读取连接数据库存配置
  5.             SqlConnection sqlcon = new SqlConnection(dbconn);

  6.             if (dataGridView1.DataSource == null || dataGridView1.CurrentRow == null)
  7.             {
  8.                 return;
  9.             }
  10.             else
  11.             {
  12.                 if (this.dataGridView1.SelectedRows.Count > 0)
  13.                 {
  14.                     DialogResult dr = MessageBox.Show("确定更新表中的数据记录? ", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  15.                     if (dr == DialogResult.OK)
  16.                     {
  17.                         try
  18.                         {
  19.                             sqlcon.Open(); ; //打开数据库连接
  20.                             foreach (DataGridViewRow row in this.dataGridView1.SelectedRows) //遍历所选中的dataGridView记录行
  21.                             {
  22.                                 string strId = row.Cells[0].Value.ToString();//取数据库的ID行记录
  23.                                 string strName = row.Cells[1].Value.ToString(); //取dataGridView1中的第2列的值
  24.                                 string strFirstname = row.Cells[2].Value.ToString();//取dataGridView1中的第3列的值
  25.                                 string strPosition = row.Cells[3].Value.ToString();//取dataGridView1中的第4列的值
  26.                                 string strPhone = row.Cells[4].Value.ToString();//取dataGridView1中的第5列的值
  27.                                 MessageBox.Show(strId+strName+strFirstname+strPosition+strPhone);
  28.                                 string sql = string.Format("update T_users set username='{0}',FirstName='{1}',Position='{2}',Telphone='{3}' where ID='{4}'", strName,strFirstname,strPosition,strPhone,strId);
  29.                                 //SQL语句来执行要求操作
  30.                                 SqlCommand cmd = new SqlCommand(sql, sqlcon); //执行sqlcommand上面sql语句
  31.                                 cmd.ExecuteNonQuery(); //执行删除操作
  32.                                 cmd.Dispose();
  33.                             }
  34.                         }
  35.                         catch (Exception ex)
  36.                         {
  37.                             MessageBox.Show(ex.ToString(), "提示");
  38.                         }
  39.                         finally
  40.                         {
  41.                             sqlcon.Close(); //关闭数据库连接
  42.                         }
  43.                         this.ButtonFresh_Click(this.t_usersTableAdapter.Fill(this.misds.T_users), null);
  44.                         //删除后执行刷新操作ButtonFresh是刷新按钮的名字
  45.                     }
  46.                     else
  47.                     {
  48.                         return;
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.         #endregion

阅读(3777) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~