分享

C#中,datagridview与sql数据源绑定后,通过控件更改数据如何自动回写更新到数...

 Cloud书屋 2012-12-19

以前一直在绕弯路,其实用c#自己的datagridview的数据绑定向导有便利的特征,就是能够简单的显示数据库中的某个表,但是问题是,对datagridview的更改无法通过简单的方法回写到数据库中。

所以我已开始尝试的是在dataGridView1_CellEndEdit事件中加入sqlcommandbuilder的方法,进行dataadapter的update();

通过实际使用,始终无法激发此更新动作,看到csdn上也有朋友发现这样的问题,我后来想了个折中的 办法,就是加了个button控件,在空间的click事件中进行处理,但是不甘心呀。

最后在偶然的情况下发现了cellvalidated事件,于是代码放入:

  1. private void dataGridVies1_CellValidated(object sender, DataGridViewCellEventArgs e)  
  2.         {  
  3.             SqlCommandBuilder cb = new SqlCommandBuilder(sda);  
  4.             sda.Update(ds);  
  5.          }  


这样,datagridview自动显示了数据源的数据,通过鼠标点击datagridview中的cell,进行更改、新增、删除行等动作都能自动更新到数据源了。休息一下;

附上源代码,希望对有相同问题的朋友有帮助:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Data.SqlClient;  
  9. using System.Globalization;  
  10. using System.IO;  
  11. using System.Data.OleDb;  
  12. using System.Runtime.InteropServices;  
  13. namespace xlstosql1  
  14. {  
  15.     public partial class Form2 : Form  
  16.     {  
  17.         public Form2()  
  18.         {  
  19.             InitializeComponent();  
  20.              
  21.         }  
  22.   
  23.         private void Form2_Load(object sender, EventArgs e)  
  24.         {  
  25.             con.ConnectionString = connectionstr;  
  26.               
  27.             sda = new SqlDataAdapter("select * from table_user", con);//定义dataadapter  
  28.             sda.Fill(ds );  
  29.             dataGridView1.DataSource = ds.Tables[0];//完成数据绑定,datagridview可以显示数据源了  
  30.               
  31.         }  
  32.         string connectionstr = "integrated security=SSPI;Initial Catalog=****;Data Source=localhost;";//初始化连接字符串  
  33.   
  34.         SqlConnection con = new SqlConnection();//  
  35.         SqlDataAdapter sda = new SqlDataAdapter();//  
  36.   
  37.         DataSet ds = new DataSet();  
  38.         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)  
  39.         {  
  40.   
  41.         }  
  42.   
  43.         private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)  
  44.         {  
  45.             //此处不能放sda的update语句,  
  46.              
  47.         }  
  48.   
  49.         private void button1_Click(object sender, EventArgs e)  
  50.         {  
  51.              
  52.         }  
  53.   
  54.         private void dataGridVies1_CellValidated(object sender, DataGridViewCellEventArgs e)  
  55.         {  
  56.             SqlCommandBuilder cb = new SqlCommandBuilder(sda);//此命令自动生成相关新增、删除、更新command  
  57.             sda.Update(ds);//更新数据源  
  58.          }  
  59.   
  60.         
  61.     }  
  62. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多