0

バインドされたデータ行を削除した後に復元する (DataGridView に行を追加する) 方法は?

DataGridView の左側にある削除キーをクリックすると、UserDeletingRow イベントが発生し、リストから削除されるエンティティ オブジェクト (AnswerTable) をキャプチャできます。

private void visitorHostDataGridView_UserDeletingRow (object sender, DataGridViewRowCancelEventArgs e)
  {

  if (e.Row.DataBoundItem is VisitorHost)
    {
    DatatoDelete datatoDelete = new DatatoDelete (e.Row, e.Row.DataBoundItem as VisitorHost);
    visitorHostsDataRowToDelete.Add (datatoDelete);
    SetModified (); // turns on save and cancel buttons
    }
}

後で保存時に削除を行います

public override void OnSave ()
{

    if (visitorHostsDataRowToDelete.Count > 0)
    {
        foreach (DatatoDelete item in visitorHostsDataRowToDelete)
        {
            _context.AnswerTables.DeleteObject (item.VisitorHostRecord as VisitorHost);
        }
    }

    visitorhostBindingSource.EndEdit ();

    _context.SaveChanges ();
    ClearModified (); // turns off save and cancel
    MessageBox.Show ("Saved");
}

繰り返しますが、私の質問は、削除をクリックした後、削除された行を復元し (削除をキャンセルして)、オブジェクトを DataGridView に戻す方法です。(コンフォメーションは必要ありません。いくつかの作業を行って、間違いを犯したことに気づき、最初からやり直したいということです) データベースに戻りたくないので、その必要はありません。データは揃っているのですが、使い方がわかりません。

アップデート:

List.Add(EntityObject) を単独で試してみましたが、うまくいきませんでした (以下を参照)。コメントがあったので再挑戦。この順序で、次の 3 つのことでコードを更新しました。

this.visitorhostBindingSource.DataSource = null;
this.visitorhostBindingSource.DataSource = _hosts;
this.visitorHostsDataGridView.Refresh ();

私が抱えていた問題を解決しているようですが、追加だけでは画面に表示されませんでした。しかし、なぜ私が行っている手順を実行する必要があるのか​​ わかりません。追加の考えは役に立ちます。

public override void OnCancel () // called from on click event of cancel button
 {

 foreach (DatatoDelete dataGridViewRow in visitorHostsDataRowToDelete)
  {
  _hosts.Add (dataGridViewRow.VisitorHostRecord);
  }
 visitorHostsDataRowToDelete.Clear();
 _hosts.Sort ();
 this.visitorhostBindingSource.DataSource = null;
 this.visitorhostBindingSource.DataSource = _hosts;
 this.visitorHostsDataGridView.Refresh ();

 ClearModified (); //disable save and cancel buttons
 }
4

2 に答える 2

0

質問で述べたように、私は List.Add(EntityObject) を単独で試してみましたが、うまくいきませんでした (以下を参照)。コメントがあったので再挑戦。この順序で、次の 3 つのことでコードを更新しました。

this.visitorhostBindingSource.DataSource = null;
this.visitorhostBindingSource.DataSource = _hosts;
this.visitorHostsDataGridView.Refresh ();

私が抱えていた問題を解決しているようですが、追加だけでは画面に表示されませんでした。しかし、なぜ私が行っている手順を実行する必要があるのか​​ わかりません。追加の考えは役に立ちます。

public override void OnCancel () // called from on click event of cancel button
 {

 foreach (DatatoDelete dataGridViewRow in visitorHostsDataRowToDelete)
  {
  _hosts.Add (dataGridViewRow.VisitorHostRecord);
  }
 visitorHostsDataRowToDelete.Clear();
 _hosts.Sort ();
 this.visitorhostBindingSource.DataSource = null;
 this.visitorhostBindingSource.DataSource = _hosts;
 this.visitorHostsDataGridView.Refresh ();

 ClearModified (); //disable save and cancel buttons
 }
于 2013-07-23T14:44:26.147 に答える
0

イベント ハンドラー メソッドでを使用returnすると、イベント ハンドラー チェーン全体ではなく、このメソッドのみが終了します。DataGridViewハンドラーが呼び出される前とその後(行が から削除される場所BindingSource)でいくつかのことを行います。

本当にイベント全体をキャンセルしたいことを述べるには、 を使用する必要がありますe.Cancel = true;

したがって、ハンドラが呼び出された後に行われたすべての処理をキャンセルするには、次のように記述します。

if(answerTableDataGridView.SelectedRows.Count>1)
    {
        // multiselect is false so we should never hit this.
        MessageBox.Show ("only one delete at a time allowed,\n Rows removed from view but NOT DELETED from the database.");
        e.Cancel = true;
        return;
    }

CancelEventArgs.Cancel ドキュメント

于 2013-04-19T08:47:22.427 に答える