C# winform 在datagridview中修改数据后,点击按钮把数据库中的数据更新,如何实现?
展开全部
/*以下是我在自己三层项目改过来的,先在设计模式下拉一dataGridView控件,名称叫dataGridView1,添加一属性text值为“修改” 属性name值为“ btnUpdate”的button控件,还有一个属性name值为btnCancel的取消控件。
以下是cs代码,把整个复制过去,改下空间名,和类名(这里指窗体名字),就行了
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace LDZ.HMISAUX
{
public partial class frmUsersEdit : Form
{
public frmUsersEdit()
{
InitializeComponent();
}
/// <summary>
/// 构造一个方法,并绑定数据到dataGridView控件中
/// </summary>
///
private void GetUsers()
{
try
{
SqlConnection conn=new SqlConnection("server=localhost;database=test;uid=abc;pwd=abc");
conn.Open();
string sql=string.Format("Select all * from testTable");
SqldataAdapter SDA=new SqldataAdapter(sql,conn);
DataSet DS = ShowUsers.GetUsers();
SDA.Fill(DS,"testTable");
this.dataGridView1.DataSource = DS.Tables[0].DefaultView;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private int Aid;
private string ALoginId;
private string AUserName;
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn=new SqlConnection("server=localhost;database=test;uid=abc;pwd=abc");
conn.Open();
string sql=string.Format("update testTable set LoginId=' "+ALoginId+" ' ,UserName=' "+AUserName;+" ' where id=' "+Aid+" ' ");
SqldataAdapter SDA=new SqldataAdapter(sql,conn);
DataSet DS = ShowUsers.GetUsers();
SDA.Fill(DS,"testTable");
this.dataGridView1.DataSource = DS.Tables[0].DefaultView;
MessageBox.Show("修改记录成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
if (dataGridView1.Rows.Count > 0)
{
Aid = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
ALoginId = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
AUserName = this.dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
AUserpwd = this.dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
AEmail = this.dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
AUserGroup = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString());
AUserType = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void UpdateUsers_Load(object sender, EventArgs e)
{
GetUsers();
}
}
}
}
以下是cs代码,把整个复制过去,改下空间名,和类名(这里指窗体名字),就行了
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace LDZ.HMISAUX
{
public partial class frmUsersEdit : Form
{
public frmUsersEdit()
{
InitializeComponent();
}
/// <summary>
/// 构造一个方法,并绑定数据到dataGridView控件中
/// </summary>
///
private void GetUsers()
{
try
{
SqlConnection conn=new SqlConnection("server=localhost;database=test;uid=abc;pwd=abc");
conn.Open();
string sql=string.Format("Select all * from testTable");
SqldataAdapter SDA=new SqldataAdapter(sql,conn);
DataSet DS = ShowUsers.GetUsers();
SDA.Fill(DS,"testTable");
this.dataGridView1.DataSource = DS.Tables[0].DefaultView;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private int Aid;
private string ALoginId;
private string AUserName;
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn=new SqlConnection("server=localhost;database=test;uid=abc;pwd=abc");
conn.Open();
string sql=string.Format("update testTable set LoginId=' "+ALoginId+" ' ,UserName=' "+AUserName;+" ' where id=' "+Aid+" ' ");
SqldataAdapter SDA=new SqldataAdapter(sql,conn);
DataSet DS = ShowUsers.GetUsers();
SDA.Fill(DS,"testTable");
this.dataGridView1.DataSource = DS.Tables[0].DefaultView;
MessageBox.Show("修改记录成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
if (dataGridView1.Rows.Count > 0)
{
Aid = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
ALoginId = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
AUserName = this.dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
AUserpwd = this.dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
AEmail = this.dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
AUserGroup = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString());
AUserType = int.Parse(this.dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void UpdateUsers_Load(object sender, EventArgs e)
{
GetUsers();
}
}
}
}
2013-11-15
展开全部
这个代码和思路很多,最简单的是通过主键去修改数据库数据表字段
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询