【问题标题】:GridView Paging, unable to get the correct Datakey valueGridView分页,无法获取正确的Datakey值
【发布时间】:2011-03-25 16:55:21
【问题描述】:

网格视图

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" PagerSettings-Position="TopAndBottom"
    PagerSettings-Mode="Numeric" PageSize="40" CellPadding="4" DataSourceID="dsEquipmentGridView"
    ForeColor="#333333" GridLines="Horizontal" Style="font-size: x-small" AutoGenerateColumns="False"
    DataKeyNames="IREF" OnRowEditing="GridView1_RowEditing" OnRowUpdated="GridView1_RowUpdated"
    OnSorted="GridView1_Sorted" OnSorting="GridView1_Sorting" OnPageIndexChanged="GridView1_PageIndexChanged1"
    OnPageIndexChanging="GridView1_PageIndexChanging" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
    OnSelectedIndexChanging="GridView1_SelectedIndexChanging" OnRowCommand="GridView1_RowCommand"
    BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" 
    OnRowCancelingEdit="GridView1_RowCancelingEdit" onload="GridView1_Load" 
    ondatabound="GridView1_DataBound">
    <PagerSettings Position="TopAndBottom" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <Columns>
 <asp:CommandField ShowDeleteButton="False" ShowSelectButton="true" EditText="QuickEdit"
     HeaderText="Manage" SelectText="Manage" />

对于加载的第一页,如果我单击命令字段,它会显示正确的 DataKey 值。

但是,如果我切换到下一页结果,则在选择记录时,相应的值不正确。它似乎保留了前一页信息。我该如何克服这个问题?

编辑:

在 GridView1_SelectedIndexChanged 方法中,我使用了以下内容:

    // Determine the index of the selected row.
    int index = GridView1.SelectedIndex;

    // Display the primary key value of the selected row.
    HttpContext.Current.Response.Write("The primary key value of the selected row is " + GridView1.DataKeys[index].Value.ToString() + ".<br>");

    GridViewRow row = GridView1.Rows[index];

    int rowindex = row.RowIndex;
    HttpContext.Current.Response.Write("rowindex-> " + rowindex.ToString() + "<br>");

    int ID = (int)GridView1.DataKeys[row.DataItemIndex - (GridView1.PageIndex * GridView1.PageSize)].Value;

    HttpContext.Current.Response.Write("row.DataItemIndex" + ID.ToString()+"<br>");

    int Userid = Convert.ToInt32(GridView1.DataKeys[index].Value);

    HttpContext.Current.Response.Write("Userid" + Userid.ToString() + "<br>");

分页后我无法获得正确的值。

【问题讨论】:

    标签: asp.net gridview paging datakey


    【解决方案1】:

    使用分页时,我们可以得到准确的行索引(在gridview中从0到totalrows计数) 而在获取datakey时,我们需要传递当前页面的行索引。

    使用下面的行获取当前页面索引并获取datakey:

    // Get the correct row index since there is paging
    int idx = gridViewRow.DataItemIndex - TestGridView.PageIndex * TestGridView.PageSize;
    

    【讨论】:

      【解决方案2】:

      我可能会迟到。 但是对于其他人,我会回复这个。

      “在使用DataKey数组之前再次重新绑定gridview”

      它对我有用。 希望它也适用于其他人。

      【讨论】:

        【解决方案3】:

        不久前已修复。正在使用第三方 GridView 助手类。这破坏了 GridView 的正常功能。

        【讨论】:

          猜你喜欢
          • 2011-02-18
          • 2010-09-18
          • 2016-06-21
          • 1970-01-01
          • 1970-01-01
          • 2014-09-21
          • 2013-08-22
          • 1970-01-01
          • 2014-11-11
          相关资源
          最近更新 更多