简体   繁体   English

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

[英]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. 我有2个文本框和1个复选框。

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. 即在图1上,我在单击“添加新行”之前,勾选了“蒂娜”的“禁用”复选框,然后消失了。

On SetPreviousDataChild()is where im having errors, these two are suggested to keep the check on the checkbox but still im having error: 在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

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

Complete C# code used : 使用完整的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++;
                }
            }
        }
    }

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 . 因此, 添加新行的概念就像它将在DataTable使用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. 这些代码行有助于在DataTable中创建新行,此处每列的值均应期望第一列为空,但在复选框的情况下,我们需要一个布尔值。 So solution is to create a column with true or false value as follow: 因此解决方案是创建具有true或false值的列,如下所示:

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

Then you cloud use Convert.ToBoolean or (bool)dt.Rows[i]["Disabled"] will work. 然后,您可以使用Convert.ToBoolean(bool)dt.Rows[i]["Disabled"]云计算。

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

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