日期:2014-05-18  浏览次数:20765 次

datagridview更新,删除问题,求代码
C# code
dataGridView1.DataSource = gv.GeiView().Tables["DATAWEI"];

DATAWEI 是数据库的表
这是datagridview的数据源
请问我运行程序时候直接在datagridview里修改删除,怎么能同步更新到数据库里
求代码

------解决方案--------------------
用sql语句更新数据库啊,,
更新后重新绑定列表
------解决方案--------------------
http://topic.csdn.net/u/20120609/16/57729ce3-fe64-425f-bd3d-1a84695e9ba3.html
参考这里!
------解决方案--------------------
遍历一次当前表啊,如果用SQL语句更新。
探讨

引用:

用sql语句更新数据库啊,,
更新后重新绑定列表

这个我理解,只是不知道如何获得datagridview的更改

------解决方案--------------------
光标所在行第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列表

}
}
}
------解决方案--------------------
探讨
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)
……