2

GridView

<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" />

For the first page loaded, it shows the correct DataKey value if I click on the command field.

However if I change to the next page of results, upon selecting a record the corresponding value is incorrect. It seems to keep the previous page information. How do I overcome this?

EDIT:

In the GridView1_SelectedIndexChanged method, I used the following:

    // 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>");

I am unable to get the correct value after paging.

leppie
  • 109,129
  • 16
  • 185
  • 292
Thomas
  • 314
  • 10
  • 28

3 Answers3

2

When using paging, we can get the exact row index (which is counted from 0 to totalrows in gridview) and when fetching datakey, we need to pass the row index of the current page.

Use the line below to get the current page index and get the datakey:

// Get the correct row index since there is paging
int idx = gridViewRow.DataItemIndex - TestGridView.PageIndex * TestGridView.PageSize;
Erik Schierboom
  • 15,025
  • 10
  • 60
  • 79
Santosh Gadge
  • 62
  • 1
  • 5
1

I may be late. But for others, i reply this.

"Rebind gridview again before using DataKey array"

it works for me. Hope it works for others too.

Kyaw Thura
  • 110
  • 1
  • 6
0

Fixed a while ago. Was using a thrid party GridView helper class. Which broke the normal functionality of the GridView.

Thomas
  • 314
  • 10
  • 28