我有一个网格视图,用户可以在想要添加新字段时添加新行,并最终保存到数据库中。 我有2个文本框和1个复选框。
问题是,当用户点击“添加新行”时,将删除上一个条目中复选框(如果用户勾选它)的检查。
,即在图1中,在我点击添加新行之前,我在Tina的Disabled复选框上勾选了它,然后它就消失了。
在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
以下是我收到代码并提供帮助的原始帖子: How can I make Excel like grid in asp.net C# Webform
使用完整的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++;
}
}
}
}
答案 0 :(得分:0)
问题是,当用户点击“添加新行”时检查
删除了上一个条目中的复选框(如果用户勾选了它)
因此, 添加新行 的概念就像它会在DataTable
中使用 null 创建一个新行。
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["ChildNo"] = i + 1;
这些代码行有助于在DataTable中创建新行,这里的每列值都是第一列为null但是在复选框的情况下我们需要一个布尔值。所以解决方案是创建一个具有true或false值的列,如下所示:
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["ChildNo"] = i + 1;
drCurrentRow["Disabled"] = false;
然后,云使用Convert.ToBoolean
或(bool)dt.Rows[i]["Disabled"]
将起作用。