2 、绑定后,调用RowEditing事件,设置gridview的EditIndex,这样就可以在新增的那行中显示文本框了,以供用户编辑数据。
首先要保证数据库中,你要修改的那个表,没有外键关系.利用SqlCommandBuilder 自动生成删改查的语句.
以下是刚写的例子:
数据库名:demo 表名:aa Sqlserver2005
button1 查询数据库 button2 更新数据库
SqlDataAdapter adapter
SqlCommandBuilder build
DataSet ds
SqlConnection con
private void button1_Click(object sender, EventArgs e)
{
con = new SqlConnection()
con.ConnectionString = @"data source = .\sqlexpressdatabase = demointegrated security = true"
SqlCommand cmd = new SqlCommand()
cmd.Connection = con
cmd.CommandText = "select * from aa"
adapter = new SqlDataAdapter(cmd)
build = new SqlCommandBuilder(adapter)
ds = new DataSet()
adapter.Fill(ds,"aa")
dataGridView1.DataSource = ds.Tables["aa"]
}
private void button2_Click(object sender, EventArgs e)
{
adapter.Update(ds.Tables["aa"])
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)