C# - Keep the checkbox checked when gridview reloads when adding new row
<p>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.</p>

<p>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.</p>

<p><em>i.e on Fig1 I ticked on the Disabled checkbox for Tina before I clicked Add New Row, then its gone.</em>
<a href="https://i.stack.imgur.com/iOqBC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iOqBC.png" alt=" "></a></p>

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

<pre><code>  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
</code></pre>

<p>Here is the original post where i got the code and help also: 
<a href="https://stackoverflow.com/questions/30273332/how-can-i-make-excel-like-grid-in-asp-net-c-sharp-webform/30274231?noredirect=1#comment69319030_30274231">How can I make Excel like grid in asp.net C# Webform</a></p>

<p>Complete C# code used :</p>

<pre><code>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 &gt; 0)
        {
            for (int i = 1; i &lt;= 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 &gt; 0)
        {
            for (int i = 0; i &lt; 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++;
                }
            }
        }
    }
</code></pre>