首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

datagridview更新,删除有关问题,求代码

2012-07-01 
datagridview更新,删除问题,求代码C# codedataGridView1.DataSource gv.GeiView().Tables[DATAWEI]DA

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)
……

热点排行