直接修改Datagridview并保存到数据库
2016-05-29 12:27
429 查看
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; using System.Data.SqlClient; namespace Quanxian { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private int job_id; private string job_desc; private int min_lvl; private int max_lvl; private void Form2_Load(object sender, EventArgs e) { binddatagridview(); } /// <summary> /// 绑定Datagridview的方法 /// </summary> private void binddatagridview() { SqlConnection sqlcon = new SqlConnection("server=.;uid=sa;pwd=;database=pubs"); SqlDataAdapter sqldap = new SqlDataAdapter("select * from jobs", sqlcon); DataSet ds = new DataSet(); sqldap.Fill(ds); this.dataGridView1.DataSource = ds.Tables[0]; } /// <summary> /// 编辑单元格后触发 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (dataGridView1.Rows.Count > 0) { job_id = int.Parse (this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString()); job_desc = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(); min_lvl = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString()); max_lvl = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString()); } } /// <summary> /// 修改 /// </summary> private void button1_Click(object sender, EventArgs e) { SqlConnection sqlcon = new SqlConnection("server=.;uid=sa;pwd=;database=pubs"); string str = "update jobs set job_desc='" + job_desc + "',min_lvl=" + min_lvl + ",max_lvl=" + max_lvl + " where job_id=" + job_id + ""; SqlCommand sqlcom = new SqlCommand(str,sqlcon); try { sqlcon.Open(); if (sqlcom.ExecuteNonQuery() > 0) MessageBox.Show("保存成功"); else MessageBox.Show("保存失败!"); } catch { //异常 } finally { sqlcon.Close();} } /// <summary> /// 刷新 /// </summary> private void button2_Click(object sender, EventArgs e) { binddatagridview(); } } }
相关文章推荐
- 介绍几个SQL SERVER中与内存相关的计数器
- debian8-install postgresql-9.4
- Oracle Dedicated server 和 Shared server(专用模式 和 共享模式) 说明
- mysql tmp_table_size优化之设置多大合适
- Hibernate 中的session 的flush、reflush 和clear 方法 ,及数据库的隔离级别
- [Sql2008错误问题]附件数据库时出现的3种常见错误的解决办法
- mysql 配置文件
- 从ORACLE RAC角度看跨数据中心的存储双活配置注意事项
- jdbcTemplate 插入Mysql 并返回主键ID
- MySql二进制连接方式详解
- oracle使用exp与imp对数据迁移备份的方法
- cmd进入 链接mysql注意事项
- 快照数据库 Snapshot DB
- MySQL字符编码设置方法
- [Sql2008错误问题] 配置系统未能初始化 0x84B10001
- oracle的3范式
- [sql2008错误问题] RegisteredServerException 异常
- redis事件
- [MongoDB]增删改查
- Mysql常用命令行大全