datagridview更新,删除问题,求代码 dataGridView1.DataSource = gv.GeiView().Tables["DATAWEI"];DATAWEI 是数据库的表这是datagridview的数据源请问我运行程序时候直接在datagridview里修改删除,怎么能同步更新到数据库里求代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用sql语句更新数据库啊,,更新后重新绑定列表 http://topic.csdn.net/u/20120609/16/57729ce3-fe64-425f-bd3d-1a84695e9ba3.html参考这里! "不返回任何键列信息的SelectCommand不支持UpdateCommand的动态SQL生成"报这个错误我的表有主键,但是不包含在我查询出来的列中 这个我理解,只是不知道如何获得datagridview的更改 遍历一次当前表啊,如果用SQL语句更新。 光标所在行第i列的值dataGridView1.CurrentRow.Cells[i].Value;//获得光标所在行的值,存放在数组中 for (int i = 0; i < dataGridView1.CurrentRow.Cells.Count; i++) { str[i] = Convert.ToString(dataGridView1.CurrentRow.Cells[i].Value); }//更新就把该行的数据Update//删除就取该行某一列的字段Delete table A(x,y) private void Delete_Click(object sender, EventArgs e) { if (ConnectionState.Closed == conn.State) { conn.Open(); string str = string.Format("delete from A where x='{0}'", dataGridView1.CurrentRow.Cells[0].Value); SqlCommand comd = new SqlCommand(); comd.Connection = conn; comd.CommandText = str; comd.ExecuteNonQuery(); conn.Close(); } } private void Update_Click(object sender, EventArgs e) { if (ConnectionState.Closed == conn.State) { conn.Open(); string str = string.Format("Update A set y='{0}' where x='{1}'",dataGridView1.CurrentRow.Cells[1].Value ,dataGridView1.CurrentRow.Cells[0].Value); SqlCommand comd = new SqlCommand(); comd.Connection = conn; comd.CommandText = str; comd.ExecuteNonQuery(); conn.Close(); } } private void btnDel_Click(object sender, EventArgs e){string key = this.txtCode.Text.Trim();foreach (DataGridViewRow row in this.dataGridView.Rows){if (row.Cells[1].Value.ToString() == key){Dictionary<string, object> p = new Dictionary<string, object>();p["主键"] = key;//执行删除sql语句//重新执行检索,更新dataGridView列表}}} c#在pictureBox上剪切图片问题 C#语音朗读问题,有没有好的轻量级的语音朗读库。[ Remoting中传递的是对象的引用如何理解? 如何用Silverlight做播放器的进度条可以拖动的 C#语音视频 C#中如何改变对话框的标题栏颜色? 小的团队开发用什么代码管理工具比较好??? 求教datagrid页眉的问题 ??在线等 请教一个很实际的问题,转移焦点问题 如何得到一个mdb数据库的所有表,每一个表的所有字段? 有一个txt文件,需要将文本读到dataset中,想请各位帮忙!!! C# 利用windowsAPI如何读取unicode形式保存的ini文件?
更新后重新绑定列表
参考这里!
dataGridView1.CurrentRow.Cells[i].Value;
//获得光标所在行的值,存放在数组中
for (int i = 0; i < dataGridView1.CurrentRow.Cells.Count; i++)
{
str[i] = Convert.ToString(dataGridView1.CurrentRow.Cells[i].Value);
}
//更新就把该行的数据Update
//删除就取该行某一列的字段Delete
private void Delete_Click(object sender, EventArgs e)
{
if (ConnectionState.Closed == conn.State)
{
conn.Open();
string str = string.Format("delete from A where x='{0}'", dataGridView1.CurrentRow.Cells[0].Value);
SqlCommand comd = new SqlCommand();
comd.Connection = conn;
comd.CommandText = str;
comd.ExecuteNonQuery();
conn.Close();
}
}
private void Update_Click(object sender, EventArgs e)
{ if (ConnectionState.Closed == conn.State)
{
conn.Open();
string str = string.Format("Update A set y='{0}' where x='{1}'",dataGridView1.CurrentRow.Cells[1].Value ,dataGridView1.CurrentRow.Cells[0].Value);
SqlCommand comd = new SqlCommand();
comd.Connection = conn;
comd.CommandText = str;
comd.ExecuteNonQuery();
conn.Close();
} }
{
string key = this.txtCode.Text.Trim();
foreach (DataGridViewRow row in this.dataGridView.Rows)
{
if (row.Cells[1].Value.ToString() == key)
{
Dictionary<string, object> p = new Dictionary<string, object>();
p["主键"] = key;//执行删除sql语句//重新执行检索,更新dataGridView列表}
}
}