我试图根据复选框列表的选择有条件地隐藏asp:gridview中的列。我有以下语法,但是这行
grdTest.Columns[othergrid5].Visible = item.Selected;
抛出错误:
指数超出范围。必须是非负数且小于集合的大小。参数名称:索引
我需要做些什么来补救?
protected void btnDoTheWork_Click(object sender, EventArgs e)
{
var columnname = string.Empty;
foreach (System.Web.UI.WebControls.ListItem item in cbxUno.Items)
{
columnname = item.Text;
var othergrid5 = GetColumnIndex(grdTest, columnname);
grdTest.Columns[othergrid5].Visible = item.Selected;
}
}
private int GetColumnIndex(GridView grid, string ColName)
{
foreach (DataControlField col in grid.Columns)
{
if (col.HeaderText.ToLower().Trim() == ColName.ToLower().Trim()) { return grid.Columns.IndexOf(col); }
}
return -1;
}