我在MainForm中有一个dataGridView,它在表单加载时将所有相关数据列入其中。
当我想去编辑一行时。我选择它并按编辑,然后加载EditForm。在这里,我可以编辑数据并保存。
信息已成功编辑和保存,但dataGridView未更新。
在关闭编辑表单时,是否有一个我没有看到的autoRefresh属性或刷新方法?
的MainForm
private void EditAdminBtn_Click(object sender, EventArgs e)
{
EditAdminForm Admin = new EditAdminForm();
Admin.idTxt.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
Admin.usernameTxt.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
Admin.firstnameTxt.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
Admin.surnameTxt.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
Admin.emailTxt.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
Admin.statusCombo.Text = this.dataGridView1.CurrentRow.Cells[6].Value.ToString();
Admin.ShowDialog();
}
public void MainForm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'student_CBDataSetAdmin.Admin' table. You can move, or remove it, as needed.
this.adminTableAdapter.Fill(this.student_CBDataSetAdmin.Admin);
}
EditForm
private void SaveBtn_Click(object sender, EventArgs e)
{
//SQL Connection and SQL for updating admin information
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
SqlDataAdapter sda3 = new SqlDataAdapter("UPDATE Admin set Admin_Username='" + this.usernameTxt.Text + "' , Admin_FName='" + this.firstnameTxt.Text + "' , Admin_SName='" + this.surnameTxt.Text + "' , Admin_Email='" + this.emailTxt.Text + "', Admin_Status='" + this.statusCombo.Text + "' WHERE Admin_ID='" + this.idTxt.Text + "'", con);
DataTable dt3 = new DataTable();
sda3.Fill(dt3);
MessageBox.Show("Information Successfully Updated!");
dt3.Clear();
this.Close();
}
THIS IS WHAT IT LOOKS LIKE, GRIDVIEW SOURCE AND BINDING AT THE BOTTOM
答案 0 :(得分:0)
问题是你没有发送任何对数据绑定对象的引用或其他什么,你是将datagridView中的值复制到弹出窗口中,然后你没有对它做任何事情。
您需要在关闭弹出窗口时返回dataTable,或者手动调用dataAdapter的刷新。第二个选项虽然需要再多次访问数据库,但更容易。
在EditAdminBtn_Click add`的末尾 编辑:根据你的截图,我看到你使用bindingSource。一旦您刷新了dataTable,就可以重置绑定
this.adminTableAdapter.Fill(this.student_CBDataSetAdmin.Admin);
AdminBindingSource.ResetBindings(false);