代码如下,点击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");
}
{
{
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");
}
{
daCountry.Update(dsCountry, "file");
{
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();
}
大哥,都说是DataGridView了哪里还有updatepanel?
应该是需要调用AcceptChanges()
定义成类变量,试一试??
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];
我用这个可以,你改成你的数据库试试看。
Button事件中又没有对数据进行操作,点Button当然还是原来的数据,看不到修改。