上面的“第二:我想通过"删除"在DataGrid上删除一条记录,然后输入数据后再按"更新"保存到数据库;”更正如下:“第二:我想通过"删除"在DataGrid上删除一条记录,然后再按"更新"保存到数据库;”

解决方案 »

  1.   

    把选中的DataGrid中的某一行的主键值取出来!
    int rowNum = dataGrid1.CurrentCell.RowNumber; 
     
    object QSMValue = dataGrid1[rowNum, 2]; 
    object ASMValue = dataGrid1[rowNum, 3];
    object CNValue  = dataGrid1[rowNum, 1];

     
    string QSM = string.Format("{0}", QSMValue); 
    string ASM = string.Format("{0}", ASMValue); 
    string CN = string.Format("{0}", CNValue);
      

  2.   

    添加,实际上就是对DataSet中的DataTable添加一新行。这个好办,对你的数据源操作就行了。
    不论是删除后的更新,还是添加后的更新都是sqlDataAdapter.Update(tempDS.tempDT)
      

  3.   

    private void Form1_Load(object sender, System.EventArgs e)
    {

    SqlConnection thisConnection = new SqlConnection(
    @"server = CELERON\SQL2000;"+
    "integrated Security = sspi;"+
    "Database = Northwind");


    string SQL = "SELECT * FROM Employees "; da = new SqlDataAdapter(SQL,thisConnection); sqlCb = new SqlCommandBuilder(da);
    da.Fill(dataSet1,"Employees"); dataGrid1.SetDataBinding(dataSet1,"Employees"); } private void button1_Click(object sender, System.EventArgs e)
    {
    da.Update(dataSet1,"Employees");
    }上面button1_Click 代码可以做你想做的
      

  4.   

    HNU(為楚有材,於我為盛!) :
    谢谢你及楼上各位的回答。我现在的目的是在dataGrid上给用户一个交互的界面。比如,用户按下了"增加"按钮,那么就要求在dataGrid上新增一个空白的记录让用户自己来填;
    如果用户按了"删除"按钮,那么,自动地在dataGrid下删除他选中的当前行。
      

  5.   

    1>add
     绑定的datagrid 在用户输入数据的时候,会在当前行的下行自动添加一个空行。
    2> del
               DataTable(DataGrid.CurrentRowIndex).Delete()
               DataTable.Table.AcceptChanges()
      

  6.   

    banni2003(木木) (你说的什么呀?
      

  7.   

    http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp
      

  8.   

    hertcloud(昨天.今天.明天→) :
      

  9.   

    我觉得
    HNU(為楚有材,於我為盛!) 
    回答的不是很好吗,如果是我,也这么写,
      

  10.   

    请问HNU(為楚有材,於我為盛!) 的回答中:
    da = new SqlDataAdapter(SQL,thisConnection);是什么意思?而且在更新时并没有真正更新到数据库。
      

  11.   

    参考:
    private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string deletecmd="delete from table where dept_id=@dept_id";
    SqlConnection myConnection =new SqlConnection(ConfigurationSettings.AppSettings["sqlconn"] ) ;
    SqlCommand myCommand=new SqlCommand(deletecmd,myConnection);
    myCommand.Parameters.Add(new SqlParameter("@dept_id",SqlDbType.VarChar ,50));
    myCommand.Parameters["@dept_id"].Value=DataGrid1.DataKeys[e.Item.ItemIndex];
    myConnection.Open();
    myCommand.ExecuteNonQuery(); 
                MyDataBind();  
    } private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    try
    {
    DataGrid1.EditItemIndex=e.Item.ItemIndex;
    MyDataBind();
    }
    catch(System.Exception errr)
    {
    Response.Write(errr.ToString() ); 
    }
    } private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    try
    {
    DataGrid1.EditItemIndex=-1;
    MyDataBind();
    }
    catch(System.Exception err)
    {
    Response.Write(err.ToString() ); 
    }
    } private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    try 
    {
    string updateCmd = "update table set dept_name=@dept_name where  dept_id=@dept_id";
    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["sqlconn"]);
    SqlCommand  myCommand = new SqlCommand(updateCmd, myConnection); myCommand.Parameters.Add(new SqlParameter("@dept_name", SqlDbType.VarChar, 50));
    myCommand.Parameters["@dept_name"].Value=((TextBox)e.Item.FindControl("edit_dept_name")).Text;   
      
    myCommand.Parameters.Add(new SqlParameter("@dept_id", SqlDbType.VarChar, 50));
    myCommand.Parameters["@dept_id"].Value =DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString() ;   
    myConnection.Open(); try 
    {
    myCommand.ExecuteNonQuery();
    }
    catch (System.Data.SqlClient.SqlException err2) 
    {
    Response.Write(err2.ToString() ); 
    }
    myConnection.Close();
    //重新绑定数据
    MyDataBind();
    //恢复到正常模式
    DataGrid1_CancelCommand(source,e);

     
    }
    catch (System.Exception err)
    {
    Response.Write(err.ToString() ); 
    }
    }
      

  12.   

    多谢楼上的。现在我已可以在dataGrid中增加或删除了。
    但更新始终不成功。也没有出错信息,就是没有保存到数据库中。请问是怎么回事?我在保存按钮中的代码是:
    dataSet11.AcceptChanges(); 
    SqlDataAdapter1.Update(dataSet11,"authors");
      

  13.   

    看如下的完整例子:
    首先取得数据,放到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();
    }
      

  14.   

    楼主可以直接操纵DataGrid绑定的DataView就行了.
      

  15.   

    hertcloud(昨天.今天.明天→) 
    我也想要份这功能的代码,我的邮箱,谢谢!