您当前的位置: 首页 >  c#

彭世瑜

暂无认证

  • 1浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#编程-75:DataGridView直接修改数据库_彭世瑜_新浪博客

彭世瑜 发布时间:2017-07-27 23:33:22 ,浏览量:1

  1. using System;
  2. using System.Data;
  3. using System.Windows.Forms;
  4. using System.Data.SqlClient;
  5. namespace DataGridViewUpdate
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         public Form1()
  10.         {
  11.             InitializeComponent();
  12.         }
  13.  
  14.         private SqlConnection GetConnection()
  15.         {
  16.             //string constr = @"Server=(localdb)\Projects;integrated security=SSPI;Initial Catalog=NewDB";
  17.             string constr=@"server=(localdb)\Projects;integrated security=sspi;database=company";
  18.             SqlConnection sqlcon = new SqlConnection(constr);
  19.             return sqlcon;       
  20.         }
  21.         private void BindData()
  22.         {
  23.              
  24.             SqlConnection sqlcon = GetConnection();
  25.             try
  26.             {
  27.                 sqlcon.Open();
  28.                 string sql = "select * from clerk";
  29.                 SqlDataAdapter sqladp = new SqlDataAdapter(sql, sqlcon);
  30.                 DataTable table = new DataTable();
  31.                 sqladp.Fill(table);
  32.                 this.dataGridView1.AutoGenerateColumns = true;
  33.                 this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
  34.                 this.dataGridView1.DataSource = table;
  35.             }
  36.             catch (Exception ex)
  37.             {
  38.  
  39.                 MessageBox.Show(ex.Message);
  40.             }
  41.             finally
  42.             {
  43.                 sqlcon.Close();
  44.             }
  45.             
  46.         }
  47.         private void Form1_Load(object sender, EventArgs e)
  48.         {
  49.             BindData();
  50.         }
  51.  
  52.         //同步更新数据库
  53.         private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  54.         {
  55.             SqlConnection sqlcon = GetConnection();
  56.             string str1 = this.dataGridView1.Columns[e.ColumnIndex].HeaderText + "=N'" + this.dataGridView1.CurrentCell.Value.ToString()+"'";
  57.             string str2 = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
  58.             try
  59.             {
  60.                 sqlcon.Open();
  61.                 string sql = "update clerk set "+str1+ "where id="+str2;
  62.                 SqlCommand sqlcom=new SqlCommand(sql,sqlcon);
  63.                 sqlcom.ExecuteNonQuery();
  64.                 BindData();
  65.                 
  66.             }
  67.             catch (Exception ex)
  68.             {
  69.  
  70.                 MessageBox.Show(ex.Message);
  71.             }
  72.             finally
  73.             {
  74.                 sqlcon.Close();
  75.             }
  76.         }
  77.     }
  78. }
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.3019s