[英]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.