2016-03-29 4 views
-1

テーブル行を更新してフォームを閉じた後、データグリッドビューを更新することでやや混乱し、開いているフォームを閉じた後、メインフォーム上の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); 

     } 

答えて

0

EditAdminForm上のデータバインディングを使用して含めて、これを行うための方法の束がありますが、最も簡単な方法は、ちょうどSaveBtn_Click方法で行を更新するために、おそらくです:フォームが閉じられたとき、これは自動的に更新をトリガします。技術的には、必ずしも必要ではないが、DataGridViewを再クエリして再投入する方が正確です。更新が成功したなら、あなたはおそらく、あなたがEditAdminFormを開いたときに、行を格納することによって、直接行のデータを更新することができます:「E」という名前のローカル変数は、この中で宣言することはできませんエラー\t 1を言って、エラーを取得

private void EditAdminBtn_Click(object sender, EventArgs e) 
{ 
    var admin = new EditAdminForm(); 
    admin.CurrentRow = dataGridView1.CurrentRow; 
    admin.ShowDialog(); 
} 

// EditAdminForm 
private DataGridRow _currentRow; 
public DataGridRow CurrentRow 
{ 
    get { return _currentRow; } 
    set 
    { 
     _currentRow = value; 
     if (value == null) 
     { 
      // TODO: Clear form 
     } 
     else 
     { 
      usernameTxt.Text = value.CurrentRow.Cells[1].Value.ToString(); 
      firstnameTxt.Text = value.CurrentRow.Cells[2].Value.ToString(); 
      surnameTxt.Text = value.CurrentRow.Cells[3].Value.ToString(); 
      emailTxt.Text = value.Cells[4].Value.ToString(); 
      statusCombo.Text = value.Cells[6].Value.ToString(); 
     } 
    } 
} 

private void UpdateCurrentRow() 
{ 
    if (_currentRow == null) return; 
    _currentRow.Cells[0].Value = idTxt.Text; 
    _currentRow.Cells[1].Value = usernameTxt.Text; 
    _currentRow.Cells[2].Value = firstnameTxt.Text; 
    _currentRow.Cells[3].Value = surnameTxt.Text; 
    _currentRow.Cells[4].Value = emailTxt.Text; 
    _currentRow.Cells[6].Value = statusCombo.Text; 
} 

private void SaveBtn_Click(object sender, EventArgs e) 
{ 
    // Save connection, same as before... 
    // then update the row 
    UpdateCurrentRow(); 
    this.Close(); 
} 
+0

これは機能しません。 currentRowとCellを使用するとエラーが発生します –

+0

詳細情報を提供する必要があります。 StackTraceとExceptionを使用してください。おそらく、それはあなたのプログラムに固有のものであることを理解する必要があるでしょう。 – Ryan

0

あなたの編集管理テーブルでは、あなたのテーブルではなく、あなたの「データベース」のものを編集します。 これは、完了したらテーブルをリロードする必要があることを意味します。

メインフォームの「閉鎖」イベントにハンドラを追加して更新するように指示します。

private void EditAdminBtn_Click(object sender, EventArgs e) 
    { 

     EditAdminForm Admin = new EditAdminForm(); 
     Admin.Closed += delegate(object sender, EventArgs e){ 
      this.studentTableAdapter.Fill(this.student_CBDataSetStudent.Student); 
      this.adminTableAdapter.Fill(this.student_CBDataSetAdmin.Admin); 
     }; 
     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(); 

    } 
+0

イムスコープは 'e 'とは異なる意味を持ちます。すでに親または現在のスコープで他の何かを表すために使用されています。 –

+0

@DBは次の' + = delegate(オブジェクト送信者、EventArgs e2)'に変更します。このメソッドにはすでに 'e'があるので、ラムダで再び使用することはできず、' e2'のような別の名前が必要です。これは学生と管理者を再クエリし、おそらく現在の行を変更することに注意してください。 – Ryan

+0

そのデータグリッドビューを更新していない –

関連する問題