我试图找到一行,然后从数据表中删除该行。我不断得到 nIndex = -1。有人有建议吗?
protected void cbxSelected_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkbox = (CheckBox)sender;
GridViewRow row = (GridViewRow)checkbox.NamingContainer;
string sSampleId = row.Cells[1].Text;
if (!string.IsNullOrEmpty(sSampleId))
{
DataTable dt;
if (ViewState["dtTemp"] != null)
{
dt = (DataTable)ViewState["dtTemp"];
}
else
{
dt = new DataTable();
dt.Columns.Add("sample_id");
}
DataRow dr;
string[] drow = new string[] { sSampleId };
dr = dt.NewRow();
dr.ItemArray = drow;
if (checkbox.Checked == true)
{
dt.Rows.Add(dr);
}
else if (checkbox.Checked == false)
{
int nIndex = dt.Rows.IndexOf(dr);
dt.Rows.RemoveAt(nIndex);
}
ViewState.Add("dtTemp", dt);
}
}