我有一个包含 500 多条记录的网格。在第一页上,当我随机选择任何项目并单击编辑时它会起作用,但是在分页到任何页面后它会给我异常
> Index was out of range. Must be non-negative and less than the size of
> the collection. Parameter name: index
我已经交叉检查:视图状态已启用,网格绑定正确。
以下是我的代码:
/// <summary>
/// Returns a comman separated value of the id selected in the grid.
/// </summary>
/// <param name="gv"></param>
/// <param name="checkbox"></param>
/// <returns></returns>
public static string GetGridViewsSelectedRowValues(GridView gv, string checkbox)
{
var sb = new StringBuilder();
if(gv.Rows.Count>0)
{
foreach (GridViewRow row in gv.Rows)
{
var cbx = (CheckBox) row.FindControl(checkbox);
if(cbx!=null && cbx.Checked)
{
var dataKey = gv.DataKeys[row.RowIndex];
if (dataKey != null) sb.Append(string.Format("{0},", dataKey.Value));
}
}
}
return sb.ToString().Remove(sb.ToString().LastIndexOf(','));
}
我的编辑按钮点击事件:
protected void btnEdit_Click(object sender, EventArgs e)
{
int count = Common.GridSelectedRows(gvCityMaster, "chkBxSelect");
if (count > 1)
{
Common.ShowMessage("Only one item can be edited at once.");
}
else
{
int id = Common.ParseInt(Common.GetGridViewsSelectedRowValues(gvCityMaster, "chkBxSelect"));
if (id > 0)
{
DisplayForm();
DisplayUserDetails(id);
}
}
}
我什至查看了 DataKeys 集合,每个集合中都有 18 个,这很好。