我有一個包含500多條記錄的網格。在第一頁,當我隨機選擇任何項目,點擊編輯,將工作,但呼叫到任何頁面後,它給了我異常索引超出了gridview及其綁定的範圍問題
> 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。
使用分頁'GridView.Rows'只返回當前頁面,不是所有頁面。 –