我绑定了一个表到DataGridView中,我现在要更新这个表,结果DataGridView中的数据更新了,但是库中的并没有更新,这个问题怎么解决?

解决方案 »

  1.   

    你确定有把DataGridView中的数据更新提交到数据库吗? 如sqlDataAapter1.Update(dataSet1);
      

  2.   

    update table set...
    用这种语句更新了吗?
      

  3.   

    我没有做Update(dateset);这步!
    就这不明白,能加我Q给我讲讲吗?
    15886725
      

  4.   

    你确定有把DataGridView中的数据更新提交到数据库吗? 如sqlDataAapter1.Update(dataSet1);
      

  5.   

    是用GridView自动生成的更新按钮吗?先试着自己写代码吧!我也是菜鸟,但是我感觉这样子比较好!下边是我的一点更新的代码,希望能对你有用!更新是自己手动书写的代码!GridView显示的数据时直接SQLConnect连接到数据库的![code=C#]SqlConnection MyCon = new SqlConnection("Data Source=ZStone;Initial Catalog=Test;User ID=sa;Password=amazing");
            MyCon.Open();        string strName = TextBox1.Text.ToString();
            string strPassWord = TextBox2.Text.ToString();
            SqlCommand theDelCommand = new SqlCommand("UPDATE [user] SET admin='"+strName+"',password='"+strPassWord+"'WHERE ID=1", MyCon);
            theDelCommand.ExecuteNonQuery();
            MyCon.Close();
            Response.Redirect("Default.aspx");[code]如果代码对你有用的话,我告诉你“当爱已成往事”是我师父!如果没有用的话!那我就不给我师父在这里丢人了!
      

  6.   

    是用GridView自动生成的更新按钮吗?先试着自己写代码吧!我也是菜鸟,但是我感觉这样子比较好!下边是我的一点更新的代码,希望能对你有用!更新是自己手动书写的代码!GridView显示的数据时直接SQLConnect连接到数据库的!SqlConnection MyCon = new SqlConnection("Data Source=ZStone;Initial Catalog=Test;User ID=sa;Password=amazing"); 
            MyCon.Open();         string strName = TextBox1.Text.ToString(); 
            string strPassWord = TextBox2.Text.ToString(); 
            SqlCommand theDelCommand = new SqlCommand("UPDATE [user] SET admin='"+strName+"',password='"+strPassWord+"'WHERE ID=1", MyCon); 
            theDelCommand.ExecuteNonQuery(); 
            MyCon.Close(); 
            Response.Redirect("Default.aspx");如果代码对你有用的话,我告诉你“当爱已成往事”是我师父!如果没有用的话!那我就不给我师父在这里丢人了!
      

  7.   

    private DataTable DT = new DataTable();
    private OleDbDataAdapter SDA = new OleDbDataAdapter();
    ...
    //初始
    OleDbCommand SCD = new OleDbCommand("select * table", conn);
    SDA.SelectCommand = SCD;
    SDA.Fill(DT);
    dataGridView1.DataSource = DT;
    ...
    //保存
    OleDbCommandBuilder SCB = new OleDbCommandBuilder(SDA);
    SDA.Update(DT);
    MessageBox.Show("数据更新成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
      

  8.   

    //同步gridview 与datatable
    this.BindingContext[dataTable].EndCurrentEdit();//提交更新的时候,更新数据库
    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();        SqlCommandBuilder commandBuilder;
    commandBuilder = new SqlCommandBuilder(sqlDataAdapter );sqlDataAdapter .Update(dataTable);
      

  9.   

    果然在这里丢人,人家问的是WinForm里的DataGridView,你拿ASP.NET的GridView来做什么?DataGridView的更新不代表后台数据集的更新,数据集的更新不代表数据库的更新,当然也可以直接更新数据库而不更新数据集,但这些都需要自己写代码,不会像ASP.NET里面那样自动生成的。通过DataGridView的编辑相关事件来对数据集或者数据库操作,记得是自己写相关代码。
      

  10.   

    如果你想在GDV中的表改变后反应到数据库中 建议你用sqlDataAdapter适配器。
    当你操作完GDV中的数据后,你可以 用sqlDataAdapter .Update(dataTable); 这方法就将你修改后的信息反馈到数据库中
      

  11.   

    我是自己写的代码!但是我写的代码只更新了DataGridView,并没有更新库,麻烦帮解决下,附代码如下:
    当点击确定按钮时执行的代码
    int scount = Convert.ToInt32(this.updateCount.Text.Trim().ToString());
                        zcount = lcount + scount;//lcount是库中有的数量scount是增加的数量
                        String str = "update drugs set drugsCount="+zcount+" where drugsId="+lid;
                        if (da.insertDrugs(str))
                        {
                            dgvBean();
                            lcount = zcount;
                            this.updateCount.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("添加失败!!!");
                            this.Close();
                        }
    这个是调用的类
    public Boolean changePass(string pass)
            {
                try
                {
                    String sql = "update login set LoginPass='" + pass + "' where id = 1";
                    OleDbCommand cmd = new OleDbCommand(sql, oc);
                    oc.Open();
                    int count = Convert.ToInt32(cmd.ExecuteNonQuery().ToString());
                    if (count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                    oc.Close();
                }        }