我有一个带Datagridview的数据输入表单,我使用的是本地database.sdf文件。当我搜索某个人的特定名称时,单击“编辑”按钮将显示一个新的更新表单。数据成功传递到更新表单
要求: 要从更新表单成功更新Person的数据,数据将在Datagridview中刷新并显示当我点击Update按钮并且Update表单Closes
错误: 我首先尝试了SqlceDataAdapter ADAP.Fill(databaseDataSet,“Mainform”); //其余的代码将显示在// // 但是当我单击“更新”按钮时,它会更新数据,但也会在DatagridView中创建整个数据的整个副本。所以我试过SqlCeDataAdapter ADAP.Update(databaseDataSet,“Mainform”);和此代码更新数据成功并且不创建任何副本,但Datagridview不刷新,仅在我关闭并再次打开应用程序时显示数据
代码尝试: 在表格1中:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
namespace HDR2
{
public partial class frmUpdate : Form
{
public string Name, Age, Gender, Family_Member, Family_Member_Name, Shnong, Dong, Grand, Amount, Phone_Number, Date_Registered, Date_Recieved, Status;
private Form1 f1;
public frmUpdate(Form1 f2)
{
InitializeComponent();
f1 = f2;
}
public void passDgvValueToForm2()
{
NametxtUpdate.Text = Name;
AgetxtUpdate.Text = Age;
GendertxtUpdate.Text = Gender;
FamilymembertxtUpdate.Text = Family_Member;
FamilymembernametxtUpdate.Text = Family_Member_Name;
ShnongtxtUpdate.Text = Shnong;
DongtxtUpdate.Text = Dong;
GrandtxtUpdate.Text = Grand;
AmounttxtUpdate.Text = Amount;
PhonenumbertxtUpdate.Text = Phone_Number;
DateregisteredUpdate.Text = Date_Registered;
daterecievedupdate.Text = Date_Recieved;
StatuscbUpdate.Text = Status;
}
private void frmUpdate_Load(object sender, EventArgs e)
{
}
private void update()
{
SqlCeConnection con = new SqlCeConnection("Data Source=Database.sdf");
con.Open();
using (SqlCeCommand cmd = new SqlCeCommand("UPDATE Mainform SET Name=@Name, Age=@Age, Gender=@Gender, Family_Member=@Family_Member, Family_Member_Name=@Family_Member_Name, Shnong=@Shnong, Dong=@Dong, Grand=@Grand, Amount=@Amount, Phone_Number=@Phone_Number, Date_Registered=@Date_Registered, Date_Recieved=@Date_Recieved, Status=@Status WHERE Name=@Name", con))
{
cmd.Parameters.AddWithValue("@Name", NametxtUpdate.Text);
cmd.Parameters.AddWithValue("@Age", AgetxtUpdate.Text);
cmd.Parameters.AddWithValue("@Gender", GendertxtUpdate.Text);
cmd.Parameters.AddWithValue("@Family_Member", FamilymembertxtUpdate.Text);
cmd.Parameters.AddWithValue("@Family_Member_Name", FamilymembernametxtUpdate.Text);
cmd.Parameters.AddWithValue("@Shnong", ShnongtxtUpdate.Text);
cmd.Parameters.AddWithValue("@Dong", DongtxtUpdate.Text);
cmd.Parameters.AddWithValue("@Grand", GrandtxtUpdate.Text);
cmd.Parameters.AddWithValue("@Amount", AmounttxtUpdate.Text);
cmd.Parameters.AddWithValue("@Phone_Number", PhonenumbertxtUpdate.Text);
cmd.Parameters.AddWithValue("@Date_Registered", DateregisteredUpdate.Text);
cmd.Parameters.AddWithValue("@Date_Recieved", daterecievedupdate.Text);
cmd.Parameters.AddWithValue("@Status", StatuscbUpdate.Text);
cmd.ExecuteNonQuery();
con.Close();
}
con.Close();
}
private void frmUpdate_FormClosed(Object sender, FormClosedEventArgs e)
{
f1.loadEmployee();
}
private void btnUpdate_Click_1(object sender, EventArgs e)
{
update();
f1.loadEmployee();
this.Close();
}
}
}
表格2 // frmUpdate:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
namespace HDR2
{
public partial class frmUpdate : Form
{
public string Name, Age, Gender, Family_Member, Family_Member_Name, Shnong, Dong, Grand, Amount, Phone_Number, Date_Registered, Date_Recieved, Status;
private Form1 f1;
public frmUpdate(Form1 f2)
{
InitializeComponent();
f1 = f2;
}
public void passDgvValueToForm2()
{
NametxtUpdate.Text = Name;
AgetxtUpdate.Text = Age;
GendertxtUpdate.Text = Gender;
FamilymembertxtUpdate.Text = Family_Member;
FamilymembernametxtUpdate.Text = Family_Member_Name;
ShnongtxtUpdate.Text = Shnong;
DongtxtUpdate.Text = Dong;
GrandtxtUpdate.Text = Grand;
AmounttxtUpdate.Text = Amount;
PhonenumbertxtUpdate.Text = Phone_Number;
DateregisteredUpdate.Text = Date_Registered;
daterecievedupdate.Text = Date_Recieved;
StatuscbUpdate.Text = Status;
}
private void frmUpdate_Load(object sender, EventArgs e)
{
}
private void update()
{
SqlCeConnection con = new SqlCeConnection("Data Source=Database.sdf");
con.Open();
using (SqlCeCommand cmd = new SqlCeCommand("UPDATE Mainform SET Name=@Name, Age=@Age, Gender=@Gender, Family_Member=@Family_Member, Family_Member_Name=@Family_Member_Name, Shnong=@Shnong, Dong=@Dong, Grand=@Grand, Amount=@Amount, Phone_Number=@Phone_Number, Date_Registered=@Date_Registered, Date_Recieved=@Date_Recieved, Status=@Status WHERE Name=@Name", con))
{
cmd.Parameters.AddWithValue("@Name", NametxtUpdate.Text);
cmd.Parameters.AddWithValue("@Age", AgetxtUpdate.Text);
cmd.Parameters.AddWithValue("@Gender", GendertxtUpdate.Text);
cmd.Parameters.AddWithValue("@Family_Member", FamilymembertxtUpdate.Text);
cmd.Parameters.AddWithValue("@Family_Member_Name", FamilymembernametxtUpdate.Text);
cmd.Parameters.AddWithValue("@Shnong", ShnongtxtUpdate.Text);
cmd.Parameters.AddWithValue("@Dong", DongtxtUpdate.Text);
cmd.Parameters.AddWithValue("@Grand", GrandtxtUpdate.Text);
cmd.Parameters.AddWithValue("@Amount", AmounttxtUpdate.Text);
cmd.Parameters.AddWithValue("@Phone_Number", PhonenumbertxtUpdate.Text);
cmd.Parameters.AddWithValue("@Date_Registered", DateregisteredUpdate.Text);
cmd.Parameters.AddWithValue("@Date_Recieved", daterecievedupdate.Text);
cmd.Parameters.AddWithValue("@Status", StatuscbUpdate.Text);
cmd.ExecuteNonQuery();
con.Close();
}
con.Close();
}
private void frmUpdate_FormClosed(Object sender, FormClosedEventArgs e)
{
f1.loadEmployee();
}
private void btnUpdate_Click_1(object sender, EventArgs e)
{
update();
f1.loadEmployee();
this.Close();
}
}
}
//请注意,我已经尝试过ADAP.Fill但它不起作用。