テーブル行を更新してフォームを閉じた後、データグリッドビューを更新することでやや混乱し、開いているフォームを閉じた後、メインフォーム上のdataGridViewを更新するにはどうすればいいですか?
私は2つのフォームを持っています。 MainForm
およびEditAdminForm
。メインフォームには、テーブルAdminをリストするdataGridViewが含まれています。 EditAdminForm
は、dataGridViewから選択されたデータ行によって取り込まれます。更新されると、私はフォームを閉じますが、テーブルは新鮮ではありません。
私はシンプルなdataGridView.Refresh();
を試しましたが、これは機能しません。
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();
}
EditAdminForm
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();
}
ロードテーブルデータ
public void MainForm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'student_CBDataSetStudent.Student' table. You can move, or remove it, as needed.
this.studentTableAdapter.Fill(this.student_CBDataSetStudent.Student);
// 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);
}
これは機能しません。 currentRowとCellを使用するとエラーが発生します –
詳細情報を提供する必要があります。 StackTraceとExceptionを使用してください。おそらく、それはあなたのプログラムに固有のものであることを理解する必要があるでしょう。 – Ryan