0

各ステートメントのヘルプが必要です。基本的に、ユーザーがセル内の値を編集すると、foreachはこれをデータグリッド内のすべてのセルに適用し、それらすべての値を変更します。foreachステートメントが機能する必要があります。データグリッドを反復処理しますが、編集された選択した行のみを変更します

try
{
    //loop through each of the rows in the dgv
    foreach  (DataGridViewRow row in dgvDetials.SelectedRows) 
    {
        int intQtyInsp = 0;

        //if quantity inspected is empty:
        if (row.Cells[2].Value.ToString() == "") 
        {
            //quantity inspected is 0. Prevents null value errors:
            intQtyInsp = 0; 
        }

        intQtyInsp = 
            Int32.Parse(dgvDetials.CurrentRow.Cells[2].Value.ToString());

        if (intQtyInsp < 0)   // checks the cell for a negative value 
        {
            intQtyInsp = 0;   // if cells is negative submits value as Zero
        }
        else
        {
            //sets quantity inspected to value entered
            intQtyInsp = Int32.Parse(row.Cells[2].Value.ToString()); 
        }

        if (intQtyInsp == 0) //if quantity inspected is 0. Ignore row.
        {

        }
        else //else gather details and insert row as production.
        {
            area = dtArea2.Rows[0]["area_code"].ToString();
            inspDate = dtpInspectionDate.Value.ToString("MM/dd/yyyy");
            inspShift = cbShift.Text;
            partNo = row.Cells[0].Value.ToString();
            // dieCode = row.Cells[0].Value.ToString();
            dieCode = "";
            machine = "";
            qtyInsp = intQtyInsp;
            qtyInspRecorded = Int32.Parse(row.Cells[5].Value.ToString());
            comment = "";
            //machine = row.Cells[3].Value.ToString();

            if (qtyInspRecorded == 0)
            {
                SQLMethods.insertProduction(area, 
                                            inspDate,
                                            inspShift, 
                                            partNo, 
                                            dieCode, 
                                            qtyInsp, 
                                            comment, 
                                            machine);
            }
            else
            {
                SQLMethods.updateProduction(area, 
                                            inspDate, 
                                            inspShift, 
                                            partNo, 
                                            dieCode, 
                                            (qtyInspRecorded + qtyInsp), 
                                            comment, 
                                            machine);
            }
        }
    }
    retrieveData(); //reset values
}
catch (Exception ex)
{
    MessageBox.Show(
        "Error instering production values. Processed with error: " 
        + ex.Message);
}    
4

1 に答える 1

1

まず、For ループから呼び出すことができるいくつかのメソッドに分割することで、コードを少し単純化します。そうすれば読みやすくなり、それによってあなたも助けやすくなります. 例を挙げると、次のとおりです。

if (intQtyInsp < 0)   // checks the cell for a negative value 
{
    intQtyInsp = 0;   // if cells is negative submits value as Zero
}
else
{
    //sets quantity inspected to value entered
    intQtyInsp = Int32.Parse(row.Cells[2].Value.ToString()); 
}

次のようなものに置き換えることができます:

int intQtyInsp = SetQuantityInspected();

次に、そのメソッドに if 構造を含めることができます。ループ内のコードの他の部分についてもこれを繰り返します。私を信じてください、これはあなたの人生を楽にします。

また、このセクションの結果は使用されないようです。の値はintQtyInspその後すぐに上書きされます!:

if (row.Cells[2].Value.ToString() == "") 
{
    //quantity inspected is 0. Prevents null value errors:
    intQtyInsp = 0; 
}

あなたの質問については、現在編集中の行の ID を取得する方法がわかりません。(可能であれば(?)、データグリッドの背後にあるテーブル/データソースをループするゲッターかもしれませんか?)。

いずれにせよ、あなたがする必要があるのは、あなたのループ内で次のようなものです:

if(IsCurrentlyEditedRow(row)){
    ...

    // (all the stuff currently in the body of your loop goes here)
    ...
}

これで、現在の行の ID が編集中の行の ID と同じかどうかに応じてorIsCurrentlyEditedRow()を返すメソッドを実装できます。TrueFalse

これが非常に具体的で詳細な回答ではない場合は申し訳ありませんが、とにかく役立つことを願っています.

于 2012-08-23T11:26:22.643 に答える