我见教程上说,在 DataGridView上更新数据的时候,他是这样做的:
当点击单元格的时候,记录行号 selectIndex.然后新建一个 DataTable origDB,并调用
origDB.ImportRow(updateData) ,其中, updateData 是在DataGridView中修改的,还没有更新到数据库中的数据.
接着用 adapter的 Update方法请求更新.我觉得这里是不是为了掩饰  DataTable的 ImportRow而已?
因为我觉得,如果在 DataGridView中修改的数据,只要还原为 DataTable 类型后,直接用 SQLDataAdapter更新不就得了....大家说是吧...

解决方案 »

  1.   

    ImportRow方法只是为了“导入行”操作,就是往DataTable里面增加一行。
      

  2.   

    嗯...这个知道..我觉得教程里面,也不算多余,我上个代码给你看:
    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;
            }