0

自分では解決できない問題があります。始める前に、皆さんにお知らせしておくことが重要です

私が最初から最後まで書いたこの問題は、あなたに私の問題が何であるかを正しく理解してもらいたいです。これを読むのが面倒な人のために、許してください。

おそらく、このフォーラムの人々が私を助けてくれるでしょう。しかし、私はすでに問題の解決策を手に入れました(ただし、ロジックだけです。コードで証明できず、どこから始めればよいかわかりません)

さて、ここに私の場合があります:

I got a Quantity column in DataGridView, and i got a feature where user could edit the Quantity column in DataGridView and automatically updating the Quantity value in the Database.

例(まだケースにあります):

I have 1000 on Quantity in the Database, and i enter the value of 50 in the Quantity column in system and after that i add to the DataGridView by clicking a "OK" button, once clicked, the Quantity in the Database should update it Quantity become 950, from 1000. Because i use a formula valueOfQuantityInDatabase - valueOfQuantityInSystemit update properly and successful

これが私の問題です:

Let's say i got a DataGridView with Quantity column and the value of it is 50 (これにより、データベースの数量値が 950 になります) and let's say customer want to add his Quantity from 50 to 150, so i change the value of Quantity in DataGridView to 150 from 50, and when i click "OK" button, the Database should update based on the formula valueOfQuantityInDatabase - valueOfQuantityInSystemand the Quantity in Database should have 850 in Quantity value (1000 - 150 のため)but it is not as i expected, the Quantity value in database is 800 (最初に追加するのは 50 であるため、データベースの数量は 950 であり、次に別の 150 を追加すると、データベースの数量は800)so it is like first entered value + second entered value.

ここに私が欲しいものがあります:

Whenever user edit the Quantity in DataGridView, it should goes to this formula valueOfQuantityInDatabase - valueOfQuantityInSystem ------ (したがって、ユーザーが DataGridView の数量値を変更するたびに、50 から 150 に変更するとしましょう。データベースの数量はそれを認識し、DataGridView の数量の現在の値 (150) を差し引く必要があります。古いものではない (50)

これが私が持ってきた私の解決策です:

  1. 変更前のデータベースの数量の最初の値を取得します (1000)

  2. 数式によってユーザーから追加された Quantity の値を取得し Quantity in DataGridView after changes - Quantity in DataGridView before changes 、それを数式で加算します Quantity in DataGridView after changes - Quantity in DataGridView before changes

しかし、解決策1または2を取得する方法がわかりません。誰か助けてもらえますか? どうもありがとうございました!あなたの答えはとても感謝しています!

4

2 に答える 2

2

デザインを変更し、データベース内の数量を更新する方法を変更することをお勧めします。あなたのグリッドビューのデータソースが何であるかわかりません。アイテムのリストの場合、ユーザーがグリッド内の数量を更新するたびに、データベースへの変更を更新せず、データ ソースを更新するだけで、注文アイテムのリストの値になる可能性があります。数量は、ユーザーが注文を完了して [保存] ボタンをクリックしたときにのみ更新する必要があります。

このアプローチでは、追加された製品の以前の数量を維持する必要はありません。ユーザーは数量をいつでも更新できます。追跡する必要はありません。コードが複雑になります。ユーザーが保存をクリックすると、グリッド内の現在の数量が取得され、データベースが更新されます。

于 2013-09-28T09:20:45.630 に答える
0

DataGridView (DGV) の任意のセルの変更をキャプチャする方法は多数あります。1 つの方法は、DGV の 2 つのイベントを利用することです。

  1. CellBeginEdit
  2. CellEndEdit

これがトリックです...

DGV を使用して値を計算するクラスで、2 つのプライベート フィールド (let、_oldValue および _newValue) を宣言できます。次に、2 つのイベントのサブスクリプションは次のようになります。

private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    _oldValue = dataGridView[e.ColumnIndex, e.RowIndex].Value;
}


private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    _newValue = dataGridView[e.ColumnIndex, e.RowIndex].Value;

    if (_newValue != _oldValue)
    {
        // YOUR LOGIC SHOULD START FROM HERE
    }
}

今、私はあなたの問題に焦点を当てています...

解決策 1 を達成したい場合は、ロジックを上記の場所に配置して、セルが初めて変更されたときにセルのデータベース値を保存することができます。

解決策2は私には十分に明確ではありませんが、上記の場所で次のことを行うだけです:

  • データベース値を保存します
  • データベース値から _newValue を差し引く
  • データベースに戻す

ただし、セルが変更されるたびにデータベースを呼び出すと、非常に時間がかかる場合があります。したがって、迅速な解決策を探す必要があります。

于 2013-09-29T13:55:50.437 に答える