Hi,
I am trying to fix code that will allow an asp.net grid view reselect its previously selected row after sorting.
I have narrowed down in debug that in the line below that passes the wrong value in into keyValue. When I hover over RowIndex for example, I will get a 2 but if i hover over .Value to the right of it I get an 18. 18 goes into keyValue and I need the 2. I just cannot get the syntax to put the 2 from .RowIndex] into the string keyValue.
string keyValue = gvFans.DataKeys[row.RowIndex].Value.ToString();
Below is my whole event handler:
protected void gvFans_DataBound(object sender, EventArgs e)
{
//reapply and reselect to row after sorting
if (ViewState["SelectedValue"] != null)
{
string SelectedValue = (string)ViewState["SelectedValue"];
//reselect the last rows selected
foreach (GridViewRow row in gvFans.Rows)
{
string keyValue = gvFans.DataKeys[row.RowIndex].Value.ToString(); //HERE***
if (keyValue == SelectedValue)
{
gvFans.SelectedIndex = row.RowIndex;
return;
}
}
}
}
I am trying to fix code that will allow an asp.net grid view reselect its previously selected row after sorting.
I have narrowed down in debug that in the line below that passes the wrong value in into keyValue. When I hover over RowIndex for example, I will get a 2 but if i hover over .Value to the right of it I get an 18. 18 goes into keyValue and I need the 2. I just cannot get the syntax to put the 2 from .RowIndex] into the string keyValue.
string keyValue = gvFans.DataKeys[row.RowIndex].Value.ToString();
Below is my whole event handler:
protected void gvFans_DataBound(object sender, EventArgs e)
{
//reapply and reselect to row after sorting
if (ViewState["SelectedValue"] != null)
{
string SelectedValue = (string)ViewState["SelectedValue"];
//reselect the last rows selected
foreach (GridViewRow row in gvFans.Rows)
{
string keyValue = gvFans.DataKeys[row.RowIndex].Value.ToString(); //HERE***
if (keyValue == SelectedValue)
{
gvFans.SelectedIndex = row.RowIndex;
return;
}
}
}
}