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

205 阅读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. }