数据在窗体的gridview里手动修改后想放到dataset里,怎么放呢?高手指教一下,不胜感激!

解决方案 »

  1.   

     DataTable dt = (DataTable)dataGridView1.DataSource;
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
      

  2.   

    如果你是用DataSet.Tables[表名]绑定的,gridview界面所有的增删改都会反映到这个表里!无需另外的代码
      

  3.   


    是DataSet.Tables[表名]绑定,但我是想把在gridview里修改后的数据(增删改插都可能有)再更新到数据库,会自己反映到DataSet.Tables[表名]里吗?我用DataSet.Tables[表名]里的数据直接更新数据库里数据就可以? 请高手再指点一下,非常感谢,我几乎就是外行 :(
      

  4.   

     //早说不就得了,非要转一个大弯?
     public void DGUpdate(String SQLString, DataGridView dataGridview,string connectionString)
            {
                
                dataGridview.EndEdit();
                SQLDataAdapter Adapter = new SQLDataAdapter();
                DataTable table = (DataTable)dataGridview.DataSource;
                using (SQLConnection connection = new SQLConnection(connectionString))
                {
                    Adapter.SelectCommand = new SQLCommand(SQLString, connection);
                    SQLCommandBuilder builder = new SQLCommandBuilder(Adapter);
                    Adapter.UpdateCommand = builder.GetUpdateCommand();
                    try
                    {
                        Adapter.Update(table);
                        table.AcceptChanges();                }                catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
     //调用
      string sql="select * from 你之前select的表"
      DGUpdate(sql, DataGridView1,connectionString)        
      

  5.   


    对,是这样的!这些知识,在MSDN上有详细说明,都是中文的,平时注意多用MSDN来辅助编程!