Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a grid view with multiple columns which allow user to fill in the data and they are able to add a new row after finishing filling the data. Among the columns, there is a column with CheckBoxList which I allow user to multiple select the option on the CheckBoxList but every time add a new row, only the first option select by the user is remain while other selection is gone. How am I able to let the option selected by the user remain while I add a new row?

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();
}


What I have tried:

I have tried code above but it only remember the one choice.
Posted
Updated 11-Oct-17 20:40pm
v2

1 solution

Hi,

You need to store the CheckBoxList value in array or jason and then set the checkbox list based on that array since its not a single value, its a set of values which checkboxlist uses. Here is example

// Create the list to store.
        List<string> YrStrList = new List<string>();
        // Loop through each item.
        foreach (ListItem item in YrChkBox.Items)
        {
            if (item.Selected)
            {
                // If the item is selected, add the value to the list.
                YrStrList.Add(item.Value);
            }
            else
            {
                // Item is not selected, do something else.
            }
        }
        // Join the string together using the ; delimiter.
        String YrStr = String.Join(";", YrStrList.ToArray());</string></string>
 
Share this answer
 
Comments
Richard Deeming 12-Oct-17 13:16pm    
"Jason" is a person's name. Did you mean JSON[^]?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900