繁体   English   中英

C#-添加新行时重新加载gridview时,请选中此复选框

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

我有一个网格视图,其中用户可以在想要添加新字段时添加新行,并最终将其保存到数据库中。 我有2个文本框和1个复选框。

问题是,当用户单击“添加新行”时,将删除上一个条目中的复选框(如果用户选中了该复选框)。

即在图1上,我在单击“添加新行”之前,勾选了“蒂娜”的“禁用”复选框,然后消失了。

在SetPreviousDataChild()上我有错误的地方,建议将这两个复选框保留在复选框上,但仍然有错误:

  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

这是我获得代码并获得帮助的原始帖子: 如何在asp.net C#Webform中使Excel像网格一样

使用完整的C#代码:

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++;
                }
            }
        }
    }

问题是,当用户单击“添加新行”时,将删除上一个条目中的复选框(如果用户选中了该复选框)。

因此, 添加新行的概念就像它将在DataTable使用null创建一个新行。

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

这些代码行有助于在DataTable中创建新行,此处每列的值均应期望第一列为空,但在复选框的情况下,我们需要一个布尔值。 因此解决方案是创建具有true或false值的列,如下所示:

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

然后,您可以使用Convert.ToBoolean(bool)dt.Rows[i]["Disabled"]云计算。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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