2016-04-01 7 views
0

フォームがロードされたときに、関連するすべてのデータをリストするMainForm内にdataGridViewがあります。データグリッドビューは、別の値の行を編集したときに更新されません。

行を編集したいとき。私はそれを選択してeditを押すと、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

問題は、あなたが送信されていないということですデータバインドされたオブジェクトへの参照、または何でもあれば、DataGridViewの値をポップアップにコピーしています。

ポップアップを閉じるときにdataTableを返すか、手動でdataAdapterの更新を呼び出す必要があります。この2番目のオプションは、データベースへのもう1回のトリップが必要ですが、簡単です。

EditAdminBtn_Click addの末尾に編集:あなたのスクリーンショットによると、私はあなたがbindingSourceを使用していることがわかります。データテーブルをリフレッシュしたら、バインディングをリセットすることができます

this.adminTableAdapter.Fill(this.student_CBDataSetAdmin.Admin); 
AdminBindingSource.ResetBindings(false); 
+0

これをeditadminボタンの最後に追加しました。今、私はdataTableのリターンを作成する必要がありますか? –

+0

通常、データベース内のデータを更新しているので、編集が完了するとデータベースから更新するだけではありません。 – Louis

+0

datagridview is notは実際に更新しています。それで、編集が行われました。編集が行われたことをデータベースで私のテーブルに表示しています。メインフォームを開くと、編集フォームが閉じられます。それは新しい価値を示すものではなく、古いものだけを示しています。 –

関連する問題