我的网页上有两个GridView - grvBookCategories
显示一个特定图书的类别,grvCategories
包含图书馆的所有可能类别。页面加载时会显示grvBookCategories
,点击按钮时会触发grvCategories
。 ItemTemplate中有cbxGrvCategories
的复选框grvCategories
。
问题是 - 如何遍历这两个GridView以在两个GridView中找到相同的值(两个都包含带有类别名称的列)?如果匹配 - 应检查相应的复选框。例如 - 如果grvBookCategories
中有3行,则还应在grvCategories
中选中3个复选框。
我尝试使用grvCategories_RowDataBound事件和foreach循环,但结果并不完全正确。问题是当前一行grvCategories
行匹配时。在这种情况下,从未选中该行的复选框,对于所有其他行,结果都很好。
protected void grvCategories_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string rowcValue = "";
string rowbcValue = "";
CheckBox cbx = new CheckBox();
foreach (GridViewRow rowc in grvCategories.Rows)
{
if (rowc.RowType == DataControlRowType.DataRow)
{
rowcValue = rowc.Cells[2].Text;
cbx = (CheckBox)rowc.Cells[3].FindControl("cbxGrvCategories");
foreach (GridViewRow rowbc in grvBookCategories.Rows)
{
if (rowbc.RowType == DataControlRowType.DataRow)
{
rowbcValue = rowbc.Cells[1].Text;
if (rowcValue.Equals(rowbcValue))
{
cbx.Checked = true;
break;
}
else
{
cbx.Checked = false;
}
}
}
}
}
}
}
提前感谢您的帮助!