代码如下,点击button后,dataGridView不能更新,求助public partial class Form1 : Form
{
    MySqlDataAdapter daCountry;
    DataSet dsCountry;
    private void Form1_Load(object sender, EventArgs e)
    {
            string myConnectionString;
            myConnectionString = "server=127.0.0.1;uid=root;pwd=123456;database=db1;";
            MySqlConnection conn = new MySqlConnection(myConnectionString);            try
            {
                string sql = "SELECT process, fileDir FROM file WHERE clientName='SIYUAN'";
                daCountry = new MySqlDataAdapter(sql, conn);
                MySqlCommandBuilder cb = new MySqlCommandBuilder(daCountry);                dsCountry = new DataSet();
                daCountry.Fill(dsCountry, "file");                dataGridView1.DataSource = dsCountry;
                dataGridView1.DataMember = "file";
            }            catch (Exception ex)
            {
                label1.Text = ex.ToString();
            }
    } private void button1_Click(object sender, EventArgs e)
        {
            daCountry.Update(dsCountry, "file");
        }
{

解决方案 »

  1.   

    Update 方法代码帖一下注意sqlconnection要及时关闭
      

  2.   

    update是MySqlDataAdapter 的一个方法,不是自己写的
      

  3.   

    daCountry.AcceptChanges();
    daCountry.Update(dsCountry, "file");
      

  4.   

    拉一个局部刷新啊,updatepanel 这个你会不会用啊
      

  5.   

      protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                GridView1.EditIndex = -1;
                this.bing();
            }        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView1.EditIndex = e.NewEditIndex;
                this.bing();
            }        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                string lendId = this.GridView1.DataKeys[e.RowIndex].Value.ToString();            string lendUser = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
                string bumen = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
                string guihuan = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();            string str = "update lendingpeople set lendName='" + lendUser + "',department='" + bumen + "',returndate='" + guihuan + "' where lend_id='" + lendId + "'";            MySqlConnection conn = new MySqlConnection();
                conn.ConnectionString = "server=localhost;uid=root;pwd=root;database=books";
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(str, conn);
                cmd.ExecuteNonQuery();
                this.GridView1.EditIndex = -1;
                this.bing();
            }
       public void bing()
            {
                MySqlConnection conn = new MySqlConnection();
                conn.ConnectionString = "server=localhost;uid=root;pwd=root;database=books";
                string st = "select *from lendingpeople";
                MySqlDataAdapter da = new MySqlDataAdapter(st, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "lendingpeople");
                this.GridView1.DataSource = ds;
                this.GridView1.DataKeyNames = new string[] { "lend_id" };
                this.GridView1.DataBind();
            }
      

  6.   


    大哥,都说是DataGridView了哪里还有updatepanel?
    应该是需要调用AcceptChanges()
      

  7.   

      dataGridView1.Refresh(),AcceptChanges()都不行啊!我原来的代码是按照mysql手册上写的,为什么不行啊!
      

  8.   

    你定义SQLCOMMANDBUILDER是局部变量??
    定义成类变量,试一试??
      

  9.   

    你update完表之后,不在绑定一下,能更新么?
      

  10.   

                MySqlConnection conn = null;
                string mySqlconnection = "Server=localhost;User Id=root;Password=123456;Persist Security Info=True;Database=student";
                conn = new MySqlConnection(mySqlconnection);            //导出数据库数据到数据集
                string sql = "select * from stu";
                System.Data.DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
                da.Fill(ds);
                //将数据集中的数据表映射到DataGridView中
                dataGridView1.DataSource = ds.Tables[0];
    我用这个可以,你改成你的数据库试试看。
      

  11.   

    你直接就在winform中显示了数据库中的数据:private void Form1_Load(object sender, EventArgs e)
    Button事件中又没有对数据进行操作,点Button当然还是原来的数据,看不到修改。
      

  12.   

    我把绑定的代码抄到button里,调试时出错
      

  13.   

    dataGridView.Refresh在事件下面加入该方法更新。
      

  14.   

    dataGridView是处于断开的,必须显示更新,用更新方法在需要的位置更新。