Jes Sie

Jes Sie

  • 699
  • 1.2k
  • 265.2k

Saving Checked Value From a Gridview With Paging to Database

Mar 26 2020 4:10 AM
Hello again,
 
I have a grid view with paging. I need to save the selected values to my database. I was able to retain the checkbox value while paging. My problem is that, when I save it to the database, it saves only the checked value of the first page of the grid view and not the succeeding pages. Please refer to my code snippets in retaining the checked vaue below:
 
  1. private void SaveCheckedValue()  
  2.         {  
  3.             ArrayList clauseIDs = new ArrayList();  
  4.             int index = -1;  
  5.   
  6.             foreach (GridViewRow row in gvClauses.Rows)  
  7.             {  
  8.                 index = (int)gvClauses.DataKeys[row.RowIndex].Value;  
  9.                 bool result = ((CheckBox)row.FindControl("cbSelect")).Checked;  
  10.   
  11.                 if (Session["Selected"] != null)  
  12.                 {  
  13.                     clauseIDs = (ArrayList)Session["Selected"];  
  14.                 }  
  15.   
  16.                 if (result)  
  17.                 {  
  18.                     if (!clauseIDs.Contains(index))  
  19.                     {  
  20.                         clauseIDs.Add(index);  
  21.                     }  
  22.                 }  
  23.                 else  
  24.                 {  
  25.                     clauseIDs.Remove(index);  
  26.                 }  
  27.             }  
  28.             if (clauseIDs != null && clauseIDs.Count > 0)  
  29.             {  
  30.                 Session["Selected"] = clauseIDs;  
  31.             }  
  32.         }  
  1. private void GetCheckedValue()  
  2.         {  
  3.             ArrayList clauseIDs = (ArrayList)Session["Selected"];  
  4.             if (clauseIDs != null && clauseIDs.Count > 0)  
  5.             {  
  6.                 foreach (GridViewRow row in gvClauses.Rows)  
  7.                 {  
  8.                     int index = (int)gvClauses.DataKeys[row.RowIndex].Value;  
  9.                     if (clauseIDs.Contains(index))  
  10.                     {  
  11.                         CheckBox cbSelect = (CheckBox)row.FindControl("cbSelect");  
  12.                         cbSelect.Checked = true;  
  13.                     }  
  14.                 }  
  15.             }  
  16.         }  
 Below is how I save to the database:
  1. private void saveCheckedItem()  
  2.         {  
  3.             foreach (GridViewRow row in gvClauses.Rows)  
  4.             {  
  5.                 if (row.RowType == DataControlRowType.DataRow)  
  6.                 {  
  7.                     CheckBox clausecode = (CheckBox)row.FindControl("cbSelect");  
  8.                     if (clausecode != null && clausecode.Checked)  
  9.                     {  
  10.                         Label lblClauseCode = (Label)row.FindControl("lblClauseCode");  
  11.                         using (SqlConnection con = DBCS.DBCon())  
  12.                         {  
  13.                             SqlCommand cmd = new SqlCommand("spNonMotor_InsertQuotationClause", con);  
  14.                             cmd.CommandType = CommandType.StoredProcedure;  
  15.                             cmd.Parameters.AddWithValue("@QuotationNo", txtQuotationNo.Text.Trim());  
  16.                             cmd.Parameters.AddWithValue("@ClauseCode", lblClauseCode.Text.Trim());  
  17.                             con.Open();  
  18.                             cmd.ExecuteNonQuery();  
  19.                         }  
  20.                     }  
  21.                 }  
  22.             }  
  23.         }  
Hope someone can guide me on how to do it. Thanks in advance. 

Answers (1)