【发布时间】:2017-03-21 20:21:41
【问题描述】:
我写信是为了寻求有关使用复选框 gridview 控件删除数据库记录的帮助。
我正在尝试使用复选框和网格视图从“ViewData”中删除记录,但“if”条件一直失败。任何关于我可能出错的地方的进一步帮助将不胜感激。非常感谢您的宝贵时间和反馈。
//Method for Deleting Record
protected void DeleteRecord(int ID)
{
SqlConnection con = new SqlConnection(cs);
SqlCommand com = new SqlCommand("delete from tbl_Users where ID=@ID", con);
com.Parameters.AddWithValue("@ID", ID);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
protected void btnDeleteRecord_Click(object sender, EventArgs e)
{
foreach (GridViewRow grow in ViewDataGrid.Rows)
{
//Searching CheckBox("chkDel") in an individual row of Grid
CheckBox chkdel = (CheckBox)grow.FindControl("chkDel");
//If CheckBox is checked than delete the record with particular empid
if (chkdel.Checked)
{
int ID = Convert.ToInt32(grow.Cells[1].Text);
DeleteRecord(ID);
}
}
//Displaying the Data in GridView
showData();
}
错误:输入字符串的格式不正确: int ID = Convert.ToInt32(grow.Cells[1].Text);
【问题讨论】:
-
grow.Cells[1]中的任何内容都无法转换为int。那个牢房里有什么? -
什么是grow.Cells[1].Text?
-
根据您的问题对我帮助很大,我没有将ID单元格放在表格中!怜悯 :) 非常感谢! :)