您现在的位置: 爱51代码网 >> 范文 >> 文章正文
DataGridView更新数据,关于DataTable的ImportRow

DataGridView更新数据,关于DataTable的ImportRow

 DataGridView上更新数据的时候,他是这样做的:
当点击单元格的时候,记录行号 selectIndex.然后新建一个 DataTable origDB,并调用
origDB.ImportRow(updateData) ,其中, updateData 是在DataGridView中修改的,还没有更新到数据库中的数据.
接着用 adapter的 Update方法请求更新.

我觉得这里是不是为了掩饰  DataTable的 ImportRow而已?
因为我觉得,如果在 DataGridView中修改的数据,只要还原为 DataTable 类型后,直接用 SQLDataAdapter更新不就得了....大家说是吧...

ImportRow方法只是为了“导入行”操作,就是往DataTable里面增加一行。 private string strConnection = @"Data Source=.\SQLExpress;Initial Catalog=DB_Person;Integrated Security=True";         private SqlConnection connect;         private SqlDataAdapter adapter;         private int selectedRow = 0;  //记录选择的单元格所在行.         private int selectedCol = 0;    //记录选择的单元格所在列.         private DataSet set;    //多次用到的DataSet.         public Form1() {             InitializeComponent();         }         //加载数据.         private void btn_LoadData_Click(object sender, EventArgs e) {             connect = new SqlConnection(strConnection);             SqlCommand cmd = connect.CreateCommand();             cmd.CommandText = "select * from [dbo].[T_Employee]";             adapter = new SqlDataAdapter(cmd);             set = new DataSet();             adapter.Fill(set, "T_Employee");             dataGV.DataSource = set.Tables["T_Employee"];             dataGV.RowHeadersVisible = false;   //行标题不可见.             dataGV.Columns[0].ReadOnly = true;  //序号不可改.         }         //更新数据的方法.         bool UpdateData() {             //注意这里的select 筛选提交要使 oriData 和 updataData 的结构相同.             string strQuery = "select * from [dbo].[T_Employee]";             DataTable oriData = GetDataTable(strQuery); //数据库中的数据.             DataTable updataData = (DataTable)dataGV.DataSource; //修改中显示在DataGridView中的数据.             oriData.Rows.Clear();             oriData.ImportRow(updataData.Rows[selectedRow]);  //获得更新行.             try {                 SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adapter);                 adapter.Update(oriData);    //更新数据.                 //oriData.AcceptChanges();    //提交更新.                 return true;    //此时更新成功.             }             catch(System.Exception ex) {                 MessageBox.Show(ex.Message);                 return false;   //此时更新失败.             }         }         //更新数据.         private void btn_UpdataData_Click(object sender, EventArgs e) {             if(UpdateData())                 MessageBox.Show("更新成功!");             else                MessageBox.Show("更新失败!");             btn_LoadData_Click(sender, e);  //重新加载数据.             dataGV.Rows[selectedRow].Selected = true; //选中刚修改的单元格.         }         //获得DataTable.         DataTable GetDataTable(string strSQL) {             adapter = new SqlDataAdapter(strSQL, connect);             DataTable dt = new DataTable();             adapter.Fill(dt);             return dt;         }         //获取选择行,列.         private void dataGV_CellClick(object sender, DataGridViewCellEventArgs e) {             selectedRow = dataGV.CurrentCell.RowIndex;             selectedCol = dataGV.CurrentCell.ColumnIndex;         }

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    undefined reference timer_crea
    linux文件/usr/lib破坏了,还原后
    linux上运行system函数时,print
    Failed to open eth0
    android手机无法与eclipse或电脑
    C/C++洗牌算法源代码
    servlet技术实现用户名唯一的验证
    E-business suite system servic
    ZOJ 3700 Ever Dream 文章中单词
    TortoiseGit和msysGit安装及使用
    ZOJ 3700 Ever Dream 文章中单词
    TortoiseGit和msysGit安装及使用
    sharepoint 2010 获取用户信息Us
    mysql主从同步延迟方案解决的学习
    生日旅行总结
    中小板生日快乐随感
    送生日快乐桑葚乳酪小蛋糕
    写给女儿的生日快乐
    总分公司财务核算
    恢复使用繁体字可行性研究报告
    对Rectangle旋转了下,但是它
    C# GetCustomAttributes怎么
    asp.net 客户端如何加密
    asp.net两个单击事件如何互相
    server.mappath如何取得服务
    在js中动态添加表格行时,怎
    asp动态网站滚动字 如何链接
    Could not load file or ass
    asp.net线程中每隔段时间执行
    ASP.net的Button的背景色为何
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |