简体   繁体   中英

C# - Keep the checkbox checked when gridview reloads when adding new row

I have a grid-view wherein a user can add a new row when he wants to add new field, and will eventually be saved to a database. I got 2 textboxes, and 1 checkbox.

Problem is, when the user clicks the "Add new row" the check on the checkbox(if the user ticked it) from the previous entry is removed.

ie on Fig1 I ticked on the Disabled checkbox for Tina before I clicked Add New Row, then its gone.

On SetPreviousDataChild()is where im having errors, these two are suggested to keep the check on the checkbox but still im having error:

  Convert.ToBoolean(dt.Rows[i]["Disabled"]); //Error: Object cannot be cast from DBNull to other types.

  ck1.Checked = (Boolean)dt.Rows[i]["Disabled"]; //Error: Specified cast is not valid

Here is the original post where i got the code and help also: How can I make Excel like grid in asp.net C# Webform

Complete C# code used :

private void bindgrdChild()
{
    DataTable dt = new DataTable();
    DataRow dr = null;

    dt.Columns.Add(new DataColumn("ChildNo", typeof(string)));
    dt.Columns.Add(new DataColumn("ChildName", typeof(string)));
    dt.Columns.Add(new DataColumn("ChildBirthdate", typeof(string)));
    dt.Columns.Add(new DataColumn("Disabled", typeof(bool)));

    dr = dt.NewRow();

    dr["ChildNo"] = 1;
    dr["ChildName"] = string.Empty;
    dr["ChildBirthdate"] = string.Empty;
    dr["Disabled"] = false;

    dt.Rows.Add(dr);
    //Store the DataTable in ViewState
    ViewState["CurrentTable9"] = dt;
    gridChild.DataSource = dt;
    gridChild.DataBind();
}
protected void ButtonAddRowChild_Click(object sender, EventArgs e)
{
    int rowIndex = 0;
    if (ViewState["CurrentTable9"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable9"];
        DataRow drCurrentRow = null;
        if (dtCurrentTable.Rows.Count > 0)
        {
            for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
            {
                //extract the TextBox values


                TextBox tx1 = (TextBox)gridChild.Rows[rowIndex].Cells[0].FindControl("txtChildName");
                TextBox tx2 = (TextBox)gridChild.Rows[rowIndex].Cells[1].FindControl("txtChildBirth");
                CheckBox ck1 = (CheckBox)gridChild.Rows[rowIndex].Cells[2].FindControl("cbChildDis");

                drCurrentRow = dtCurrentTable.NewRow();

                drCurrentRow["ChildNo"] = i + 1;
                dtCurrentTable.Rows[i - 1]["ChildName"] = tx1.Text;
                dtCurrentTable.Rows[i - 1]["ChildBirthdate"] = tx2.Text;
                dtCurrentTable.Rows[i - 1]["Disabled"] = ck1.Checked;

                rowIndex++;
            }
            dtCurrentTable.Rows.Add(drCurrentRow);
            ViewState["CurrentTable9"] = dtCurrentTable;

            gridChild.DataSource = dtCurrentTable;
            gridChild.DataBind();
        }
    }
    else
    {
        Response.Write("ViewState is null");
    }

    SetPreviousDataChild();
}

private void SetPreviousDataChild()
{
    int rowIndex = 0;
    if (ViewState["CurrentTable9"] != null)
    {
        DataTable dt = (DataTable)ViewState["CurrentTable9"];
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                TextBox tx1 = (TextBox)gridChild.Rows[rowIndex].Cells[0].FindControl("txtChildName");
                TextBox tx2 = (TextBox)gridChild.Rows[rowIndex].Cells[1].FindControl("txtChildBirth");
                CheckBox ck1 = (CheckBox)gridChild.Rows[rowIndex].Cells[2].FindControl("cbChildDis");

                tx1.Text = dt.Rows[i]["ChildName"].ToString();
                tx2.Text = dt.Rows[i]["ChildBirthdate"].ToString();
//Here is where im having errors, these two are suggested to keep the check on the checkbox but still im having error
                Convert.ToBoolean(dt.Rows[i]["Disabled"]); //Error: Object cannot be cast from DBNull to other types.
                ck1.Checked = (Boolean)dt.Rows[i]["Disabled"]; //Error: Specified cast is not valid


                    rowIndex++;
                }
            }
        }
    }

Problem is, when the user clicks the "Add new row" the check on the checkbox(if the user ticked it) from the previous entry is removed.

So concept of add new row is like it will create one new row in DataTable with null .

drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["ChildNo"] = i + 1;

These line of code is helping creating new row in DataTable , here value for each column expect first column is null but in case of checkbox we need one Boolean value. So solution is to create a column with true or false value as follow:

drCurrentRow = dtCurrentTable.NewRow();
 drCurrentRow["ChildNo"] = i + 1;
 drCurrentRow["Disabled"] = false;

Then you cloud use Convert.ToBoolean or (bool)dt.Rows[i]["Disabled"] will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM