2017-10-11 13 views
0

J'ai une vue de la grille avec plusieurs colonnes qui permettent à l'utilisateur de remplir les données et ils sont en mesure d'ajouter une nouvelle ligne après avoir fini de remplir les données. Parmi les colonnes, il y a une colonne avec CheckBoxList que je permets à l'utilisateur de sélectionner plusieurs fois l'option sur CheckBoxList mais chaque fois que j'ajoute une nouvelle ligne, seule la première option sélectionnée par l'utilisateur reste alors que l'autre sélection est perdue. Comment puis-je laisser l'option sélectionnée par l'utilisateur pendant que j'ajoute une nouvelle ligne?CheckBoxList dans GridView seulement Rappelez-vous la première option cochée quand une nouvelle ligne ajoutée

private void SetPreviousDataLecturer() 
{ 
    int rowIndex = 0; 
    if (ViewState["LecturerGridView"] != null) 
    { 
     DataTable dataTableCurrent = (DataTable)ViewState["LecturerGridView"]; 
     if (dataTableCurrent.Rows.Count > 0) 
     { 
      for (int i = 0; i < dataTableCurrent.Rows.Count; i++) 
      { 
       TextBox textBoxLName = (TextBox)LecturerGridView.Rows[rowIndex].Cells[1].FindControl("LecturerName"); 
       TextBox textBoxLID = (TextBox)LecturerGridView.Rows[rowIndex].Cells[2].FindControl("LecturerID"); 
       TextBox textBoxLAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[3].FindControl("LecturerAddress"); 
       TextBox textBoxLPNumber = (TextBox)LecturerGridView.Rows[rowIndex].Cells[4].FindControl("LecturerPNumber"); 
       TextBox textBoxLEAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[5].FindControl("LecturerEAddress"); 
       CheckBoxList checkBoxListLCourse = (CheckBoxList)LecturerGridView.Rows[rowIndex].Cells[6].FindControl("LecturerCourse"); 
       TextBox textBoxLPassword = (TextBox)LecturerGridView.Rows[rowIndex].Cells[7].FindControl("LecturerPassword"); 

       LecturerGridView.Rows[i].Cells[0].Text = Convert.ToString(i + 1); 
       textBoxLName.Text = dataTableCurrent.Rows[i]["LecturerName"].ToString(); 
       textBoxLID.Text = dataTableCurrent.Rows[i]["LecturerID"].ToString(); 
       textBoxLAdd.Text = dataTableCurrent.Rows[i]["LecturerAddress"].ToString(); 
       textBoxLPNumber.Text = dataTableCurrent.Rows[i]["LecturerPNumber"].ToString(); 
       textBoxLEAdd.Text = dataTableCurrent.Rows[i]["LecturerEAddress"].ToString(); 
       checkBoxListLCourse.SelectedValue = dataTableCurrent.Rows[i]["LecturerCourse"].ToString(); 
       textBoxLPassword.Text = dataTableCurrent.Rows[i]["LecturerPassword"].ToString(); 
       rowIndex++; 
      } 
     } 
    } 
} 

private void AddNewRowToLecturerGV() 
{ 
    int rowIndex = 0; 
    if (ViewState["LecturerGridView"] != null) 
    { 
     DataTable dataTableCurrent = (DataTable)ViewState["LecturerGridView"]; 
     DataRow dataRowCurrent = null; 
     if (dataTableCurrent.Rows.Count > 0) 
     { 
      for (int i = 1; i <= dataTableCurrent.Rows.Count; i++) 
      { 
       TextBox textBoxLName = (TextBox)LecturerGridView.Rows[rowIndex].Cells[1].FindControl("LecturerName"); 
       TextBox textBoxLID = (TextBox)LecturerGridView.Rows[rowIndex].Cells[2].FindControl("LecturerID"); 
       TextBox textBoxLAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[3].FindControl("LecturerAddress"); 
       TextBox textBoxLPNumber = (TextBox)LecturerGridView.Rows[rowIndex].Cells[4].FindControl("LecturerPNumber"); 
       TextBox textBoxLEAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[5].FindControl("LecturerEAddress"); 
       CheckBoxList checkBoxListLCourse = (CheckBoxList)LecturerGridView.Rows[rowIndex].Cells[6].FindControl("LecturerCourse"); 
       TextBox textBoxLPassword = (TextBox)LecturerGridView.Rows[rowIndex].Cells[7].FindControl("LecturerPassword"); 

       dataRowCurrent = dataTableCurrent.NewRow(); 
       dataRowCurrent["RowNumber"] = i + 1; 
       dataTableCurrent.Rows[i - 1]["LecturerName"] = textBoxLName.Text; 
       dataTableCurrent.Rows[i - 1]["LecturerID"] = textBoxLID.Text; 
       dataTableCurrent.Rows[i - 1]["LecturerAddress"] = textBoxLAdd.Text; 
       dataTableCurrent.Rows[i - 1]["LecturerPNumber"] = textBoxLPNumber.Text; 
       dataTableCurrent.Rows[i - 1]["LecturerEAddress"] = textBoxLEAdd.Text; 
       dataTableCurrent.Rows[i - 1]["LecturerCourse"] = checkBoxListLCourse.SelectedValue.ToString(); 
       dataTableCurrent.Rows[i - 1]["LecturerPassword"] = textBoxLPassword.Text; 

       rowIndex++; 
      } 

      dataTableCurrent.Rows.Add(dataRowCurrent); 
      ViewState["LecturerGridView"] = dataTableCurrent; 

      LecturerGridView.DataSource = dataTableCurrent; 
      LecturerGridView.DataBind(); 
     } 
    } 
    else 
    { 
     Response.Write("ViewState is null."); 
    } 
    SetPreviousDataLecturer(); 
} 
+0

pouvez-vous s'il vous plaît envoyer le code complet, y compris le fichier aspx – Arshad

+0

Salut. @Arshad j'ai inclus le aspx et aspx.cs. S'il vous plaît laissez-moi savoir si vous avez besoin de plus. Merci. –

+0

Est-ce un fichier .cs complet? S'il vous plaît inclure le fichier .cs complet afin que je puisse voir votre événement de chargement de la page aussi – Arshad

Répondre

0

Ma réponse. Cette réponse a un problème, comme la liste de cases à cocher qui défilera automatiquement vers le haut lorsque nous cochez quelque chose dans la liste de cases à cocher.

private void SetPreviousDataLecturer() 
{ 
    int rowIndex = 0; 

    if (ViewState["LecturerGridView"] != null) 
    { 
     DataTable dataTableCurrent = (DataTable)ViewState["LecturerGridView"]; 
     if (dataTableCurrent.Rows.Count > 0) 
     { 
      for (int i = 0; i < dataTableCurrent.Rows.Count; i++) 
      { 
       TextBox textBoxLName = (TextBox)LecturerGridView.Rows[rowIndex].Cells[1].FindControl("LecturerName"); 
       TextBox textBoxLID = (TextBox)LecturerGridView.Rows[rowIndex].Cells[2].FindControl("LecturerID"); 
       TextBox textBoxLAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[3].FindControl("LecturerAddress"); 
       TextBox textBoxLPNumber = (TextBox)LecturerGridView.Rows[rowIndex].Cells[4].FindControl("LecturerPNumber"); 
       TextBox textBoxLEAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[5].FindControl("LecturerEAddress"); 
       CheckBoxList checkBoxListLCourse = (CheckBoxList)LecturerGridView.Rows[rowIndex].Cells[6].FindControl("LecturerCourse"); 
       TextBox textBoxLPassword = (TextBox)LecturerGridView.Rows[rowIndex].Cells[7].FindControl("LecturerPassword"); 

       LecturerGridView.Rows[i].Cells[0].Text = Convert.ToString(i + 1); 
       textBoxLName.Text = dataTableCurrent.Rows[i]["LecturerName"].ToString(); 
       textBoxLID.Text = dataTableCurrent.Rows[i]["LecturerID"].ToString(); 
       textBoxLAdd.Text = dataTableCurrent.Rows[i]["LecturerAddress"].ToString(); 
       textBoxLPNumber.Text = dataTableCurrent.Rows[i]["LecturerPNumber"].ToString(); 
       textBoxLEAdd.Text = dataTableCurrent.Rows[i]["LecturerEAddress"].ToString(); 
       checkBoxListLCourse.Text = dataTableCurrent.Rows[i]["LecturerCourse"].ToString(); 
       string lecturerCourse = dataTableCurrent.Rows[i]["LecturerCourse"].ToString(); 
       if (!string.IsNullOrEmpty(lecturerCourse)) 
       { 
        for (int j = 0; j < lecturerCourse.Split(',').Length; j++) 
        { 
         checkBoxListLCourse.Items.FindByValue(lecturerCourse.Split(',')[j].ToString()).Selected = true; 
        } 
       } 
       textBoxLPassword.Text = dataTableCurrent.Rows[i]["LecturerPassword"].ToString(); 
       rowIndex++; 
      } 
     } 
    } 
} 

private void AddNewRowToLecturerGV() 
{ 
    int rowIndex = 0; 

    if (ViewState["LecturerGridView"] != null) 
    { 
     DataTable dataTableCurrent = (DataTable)ViewState["LecturerGridView"]; 
     DataRow dataRowCurrent = null; 
     if (dataTableCurrent.Rows.Count > 0) 
     { 
      for (int i = 1; i <= dataTableCurrent.Rows.Count; i++) 
      { 
       TextBox textBoxLName = (TextBox)LecturerGridView.Rows[rowIndex].Cells[1].FindControl("LecturerName"); 
       TextBox textBoxLID = (TextBox)LecturerGridView.Rows[rowIndex].Cells[2].FindControl("LecturerID"); 
       TextBox textBoxLAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[3].FindControl("LecturerAddress"); 
       TextBox textBoxLPNumber = (TextBox)LecturerGridView.Rows[rowIndex].Cells[4].FindControl("LecturerPNumber"); 
       TextBox textBoxLEAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[5].FindControl("LecturerEAddress"); 
       CheckBoxList checkBoxListLCourse = (CheckBoxList)LecturerGridView.Rows[rowIndex].Cells[6].FindControl("LecturerCourse"); 
       TextBox textBoxLPassword = (TextBox)LecturerGridView.Rows[rowIndex].Cells[7].FindControl("LecturerPassword"); 

       dataRowCurrent = dataTableCurrent.NewRow(); 
       dataRowCurrent["RowNumber"] = i + 1; 
       dataTableCurrent.Rows[i - 1]["LecturerName"] = textBoxLName.Text; 
       dataTableCurrent.Rows[i - 1]["LecturerID"] = textBoxLID.Text; 
       dataTableCurrent.Rows[i - 1]["LecturerAddress"] = textBoxLAdd.Text; 
       dataTableCurrent.Rows[i - 1]["LecturerPNumber"] = textBoxLPNumber.Text; 
       dataTableCurrent.Rows[i - 1]["LecturerEAddress"] = textBoxLEAdd.Text; 
       string lecturerCourse = string.Empty; 
       foreach (ListItem item in checkBoxListLCourse.Items) 
       { 
        if (item.Selected) 
        { 
         if (!string.IsNullOrEmpty(lecturerCourse)) 
         { 
          lecturerCourse += ","; 
         } 
         lecturerCourse += item.Value; 
        } 
       } 
       dataTableCurrent.Rows[i - 1]["LecturerCourse"] = lecturerCourse; 
       dataTableCurrent.Rows[i - 1]["LecturerPassword"] = textBoxLPassword.Text; 
       rowIndex++; 
      } 
      dataTableCurrent.Rows.Add(dataRowCurrent); 
      ViewState["LecturerGridView"] = dataTableCurrent; 

      LecturerGridView.DataSource = dataTableCurrent; 
      LecturerGridView.DataBind(); 
     } 
    } 
    else 
    { 
     Response.Write("ViewState is null."); 
    } 
    SetPreviousDataLecturer(); 
} 
0

Vous devez maintenir l'état pour la case sélectionnée. Sur le bouton, cliquez sur "Ajouter une nouvelle ligne" d'abord obtenir l'état de chaque ligne dans un DataTable et ajouter une ligne vide puis remplir ce DataTable.

Vous devez également conserver l'état de l'élément sélectionné. Vous pouvez obtenir des valeurs sélectionnées dans un fichier CSV comme:

string selectedItems = String.Join(",", 
checkBoxListLCourse.Items.OfType<ListItem>().Where(r => r.Selected) 
    .Select(r => r.Value)); 

et vous pouvez restaurer comme:

 string[] items = selectedItems.Split(','); 
     for (int i = 0; i < checkBoxListLCourse.Items.Count; i++) 
     { 
      if (items.Contains(checkBoxListLCourse.Items[i].Value)) 
      { 
       checkBoxListLCourse.Items[i].Selected = true; 
      } 
     } 
+0

Salut. @Arshad Merci pour votre réponse. Sentez-vous si désolé de demander mais où devrais-je mettre votre réponse? –