0

我有一个包含 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 个,这很好。

4

3 回答 3

0

GridView.Rows只返回当前页面的所有行。您可以使用ViewState将逗号分隔的 ID 值存储在 GridView 中。

private string SelectedIDs{
    get {
        if (ViewState("SelectedIDs") == null {
            ViewState("SelectedIDs") = "";
        }
        return (String)ViewState("SelectedIDs");
    }
    set { ViewState("SelectedIDs") = value; }
}

您需要更新中的字符串PageIndexChanging

于 2012-10-08T10:13:51.567 回答
0

上面的解决方案在分页时对我不起作用(使用 C# .Net 4.5),所以我想出了另一个解决方案。

注意:我在选择而不是按钮(第一个功能)时使该行可编辑。

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
    foreach (GridViewRow row in gridView.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(gridView, "Select$" + row.DataItemIndex, true);
        }
    }
    base.Render(writer);
}

protected void trackingDataGV_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gridView.PageIndex = e.NewPageIndex;
    getGridViewData();
}

protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
        int pageSize = gridView.PageSize;
        int pageIndex = gridView.PageIndex;
        int rowIndex = Convert.ToInt32(e.CommandArgument);
        int newRowIndex = 0;

        if (pageIndex > 0)
        {
            newRowIndex = pageIndex * pageSize;
            rowIndex = rowIndex - newRowIndex;
        }

    string valueXYZ = ((HiddenField)gridView.Rows[rowIndex].FindControl("valueXYZ")).Value;
    }
}
于 2013-03-14T08:27:26.633 回答
0

如果您在模板字段中有一个复选框:

<asp:GridView ID="EditListGridView" runat="server" AutoGenerateColumns="false"  AllowPaging="true" AllowSorting="true" PageSize="3" >
    <Columns>
      <asp:TemplateField>
        <ItemTemplate>
          <asp:CheckBox ID="Include" runat="server" OnCheckedChanged="Checkbox_Click"  AutoPostBack="true"  />
        </ItemTemplate>
      </asp:TemplateField>

要获取总索引,请使用:

int i = ((GridViewRow)((Control)sender).Parent.Parent).DataItemIndex;

要获取当前页面上的索引,请使用:

int i = ((GridViewRow)((Control)sender).Parent.Parent).RowIndex;

要在分页网格视图中获取复选框(在当前页面上使用索引):

CheckBox cb = (CheckBox)EditListGridView.Rows[i].FindControl("Include");
于 2015-07-21T14:58:58.843 回答