1

こんにちは、1回の保存クリックですべてのグリッドビュー値を保存しようとしています。

これが私が試した私のコードです。

protected void imgbtnSave_Click(object sender, ImageClickEventArgs e)
{
     // DAL and Other Code here

 foreach (GridViewRow row in gvServicePort.Rows)
    {
      Label _lblOneID = gvServicePort.Rows[row.RowIndex].FindControl("lblOneID") as Label;
        objserRtnDtls.TerminalID = Convert.ToInt16(_lblOneID .Text);

     RadioButton _lblTwo = gvServicePort.Rows[row.RowIndex].FindControl("rdDirectPOL") as RadioButton;   

         if (_lblIsPOL.Checked == true)
            objserRtnDtls.IsPOL = true;
        else
            objserRtnDtls.IsPOL = false;  
         int i = dalSPM.ServicePort_Update(objserRtnDtls, objSerRtn);
}

しかし、ここでは最初の 2 回の反復で戻り値が 1 になり、その後 0 になり、DB に保存されません。

4

1 に答える 1

0

1 つの問題は、_lblTwo が objserRtnDtls の設定に使用されないことです。おそらく_lblIsPOLを意図していたと思います。その前提に基づいて、以下のコードを調整しました。

上記のコメントに従って DataRow のチェックも含めました (優れた点 mshsayem)。

protected void imgbtnSave_Click(object sender, ImageClickEventArgs e)
{
    // DAL and Other Code here

    foreach (GridViewRow row in gvServicePort.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            // wire up the controls in this row
            Label _lblOneID = (Label)row.FindControl("lblOneID");
            RadioButton _rdIsPOL = (RadioButton)row.FindControl("rdDirectPOL"); // renamed _lblTwo to _rdIsPOL

            // load the control data into your object
            objserRtnDtls.TerminalID = Convert.ToInt16(_lblOneID.Text);
            objserRtnDtls.IsPOL = _rdIsPOL.Checked; // renamed _lblIsPOL to _rdIsPOL to match the RadioButton above

            // attempt to update the database
            int i = dalSPM.ServicePort_Update(objserRtnDtls, objSerRtn);
        }
    }
}

もう 1 つの問題は、各 foreach 反復の最後に objserRtnDtls をリセットすることを検討して、オブジェクトの最終行の古いデータを誤って残さないようにすることです。重複データがあり、挿入しようとすると、TerminalID がデータベース内で一意であると想定されている場合、失敗する可能性があります。objSerRtn が何をするかわからないので、自分で判断する必要があります。

于 2013-02-14T07:19:04.757 に答える