填充数据的代码:
string constring = "";
con = new SqlConnection(constring);
con.Open();
DataSet ds = new DataSet();
string SQL = "select * from table";
SqlDataAdapter sda = new SqlDataAdapter(SQL, con);
sda.UpdateCommand = new SqlCommand("update from student", con);
sda.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0];我想更新DataGridView中修改了的数据,于是写了sda.UpDate(ds);结果提示我没有指定UpdateCommand. 请问如何指定它呢?相应的SQL怎么写?小弟新手这里谢过了。

解决方案 »

  1.   

    this.dataGridView1.DataMember = "table";
      

  2.   

    http://community.csdn.net/Expert/topic/5184/5184602.xml?temp=.4911768
      

  3.   

    555 你还是不明白... 我的修改事件没有执行成功,提示我没有指定UpdateCommand.
      

  4.   

    楼主其实我觉得sda.UpdateCommand = new SqlCommand("update from student", con);这句话可以不要,直接这样:
    string constring = "";
    con = new SqlConnection(constring);
    con.Open();
    DataSet ds = new DataSet();
    string SQL = "select * from table";
    SqlDataAdapter sda = new SqlDataAdapter(SQL, con);
    sda.Fill(ds);
    this.dataGridView1.DataSource = ds.Tables[0];
    就可以了,因为你修改了之后,重新加载数据库中的数据就可以了嘛。
    希望对你有帮助。
      

  5.   

    sda.UpdateCommand = new SqlCommand("update from student", con);
    这句UPDATE语句有语法错误
      

  6.   

    恩 cmHua说的对,这个地方我笔误了。但是没有这UpdateCommand的时候不能更新的。
    这个UpdateCommand应该如何设置?
      

  7.   

    update tablename set fieldname="" ;//where
      

  8.   

    dataAdpater.UpdateCommand = new SqlCommand(
       "UPDATE Categories SET CategoryName = @CategoryName " +
       "WHERE CategoryID = @CategoryID" , connection);dataAdpater.UpdateCommand.Parameters.Add(
       "@CategoryName", SqlDbType.NVarChar, 15, "CategoryName");SqlParameter parameter = dataAdpater.UpdateCommand.Parameters.Add(
      "@CategoryID", SqlDbType.Int);
    parameter.SourceColumn = "CategoryID";
    parameter.SourceVersion = DataRowVersion.Original;
      

  9.   

    更新的话,ds.reset(),然后再da.fill(),然后再设置datasouse就可以了
      

  10.   

    sfateam的是正解,如果你不想写UpdateCommand,也可以用下面这句代替SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(sda);
    它为替你自动生成UpdateCommand语句