有个最简单的方法
就是建立一个XML 数据集,通过操作数据集来操作。最后统一更新数据集

解决方案 »

  1.   

    你插入的代码是什么?会不会是那里错了?
    你连续加入多行没有主健吗?按理说是可以的。
    关于3.
    iRow=dg.CurrentRowIndex;
      

  2.   

    看如下的方法:
    首先取得数据,放到DataGrid里System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");
    conn.Open();
    System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student",conn);
    dt = new System.Data.DataSet();
    da.Fill(dt,"student");然后绑定数据集和DataGrid
    DataGrid.SetDataBinding(dt,"student");
    如果需要,可以绑定TextBox来作录入,而用DataGrid显示
    this.textBox16.DataBindings.Add("Text",dt,"student.stuno");
    然后进行数据的操作如:
    增加:
    this.BindingContext[dt,"student"].AddNew();
    删除:
    this.BindingContext[dt,"student"].RemoveAt(this.BindingContext[dt,"student"].Position);
    最后把结果写回数据库:
    // sqlInsertCommand1
    // 
    this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name) VALUES (@stuno, @name)"; 
    this.sqlInsertCommand1.Connection = this.conn;
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
    // 
    // sqlUpdateCommand1
    // 
    this.sqlUpdateCommand1.CommandText = "UPDATE student SET stuno = @stuno, name = @name WHERE (stuno = @Original_stuno)";
    this.sqlUpdateCommand1.Connection = this.conn;
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));
     
    // sqlDeleteCommand1
    // 
    this.sqlDeleteCommand1.CommandText = "DELETE FROM student WHERE (stuno = @Original_stuno)";
    this.sqlDeleteCommand1.Connection = this.conn;
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));

    this.sqlDa.DeleteCommand = this.sqlDeleteCommand1;
    this.sqlDa.InsertCommand = this.sqlInsertCommand1;
    this.sqlDa.UpdateCommand = this.sqlUpdateCommand1;
    try
    {
    sqlDa.Update(dt.GetChanges,"student"); 
    return true;
    }
    catch(System.Data.SqlClient.SqlException ex)
    {

    return false;

    finally
    {
    conn.Close();
    }
      

  3.   

    楼主在更新数据库之前要通知界面停止编辑。
    // 停止当前的任何编辑。
    this.BindingContext[ds1,"tableName"].EndCurrentEdit();如果上面不行那就这样做吧;
    this.BindingContext[ds1,"tableName"].Position = 0;
    this.BindingContext[ds1,"tableName"].Position = this.BindingContext[ds1,"tableName"].Count-1;
    上面的代码好比是在DataGrid上的小箭头移动了一下。
      

  4.   

    @是SQl数据提供者参数定义标志!
      

  5.   

    可以使用循环删除方法,先用for循环检测每一行的删除条件(如column.checkbox.selected=true),如果符合条件就删除,这个方法比较苯,但是我暂时想不到好的方法了.
      

  6.   

    to:TwoEyes完成数据的插入后,加:myAdapter.InsertCommand.ExecuteNonQuery();
    使所有的操作生效
      

  7.   

    在 snof(雪狼)的回答中
    对stuno我不想手工在datagrid中录入,我想那列隐藏,动态的赋值,即stuno的值是递增的。
    怎么实现????