GridView的更新
private DataTable dTable;
private SqlDataAdapter sDAdapter;
private SqlConnection conn;
private void Form1_Load(object sender, EventArgs e)
{
string connString = "server=127.0.0.1;database=northwind;uid=sa;pwd=123";
conn = new SqlConnection(connString);
sDAdapter = new SqlDataAdapter("select * from myTable", conn);//myTable中有"userID"和"userName"两个列
builder = new SqlCommandBuilder(sDAdapter);
dTable = new DataTable();
sDAdapter.Fill(dTable);
dataGridView1.DataSource = dTable;
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//我想在对单元格进行完编辑后,实现对数据库的更新
//sDAdapter.Update(dTable);
}
------解决方案--------------------
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string userID = this.GridView1.Rows[e.NewEditIndex].Cells[0].Text;//如果你的 userID是在GridView的第一列时,否则以此类推
string userName = this.GridView1.Rows[e.NewEditIndex].Cells[1].Text;//如果你的 userName是在GridView的第二列时,否则以此类推
string updatesql = "update myTable set userID=" + userID + " ,set userName='" + userName + "'where userID=" + userID + "";
SqlConnection conn = new SqlConnection("server=127.0.0.1;database=northwind;uid=sa;pwd=123");
conn.Open();
SqlCommand cmd = new SqlCommand(updatesql, conn);
cmd.ExecuteNonQuery();
conn.Close();
Bind();
}