Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbee cannot get the correct value in string

Status
Not open for further replies.

Gzk2010

Programmer
Aug 17, 2010
48
US
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;
}

}
}
}
 
This might be a long shot - but have you tried making keyValue an int instead?

I had some problems the other day with values not being correct, and it turned out the reason was that I used a string instead of an int.
 
Yes I have. But it still is trying to put 18 in it, instead of 2 what is in RowIndex when I hover over it in debug, which is what I need to make it work correctly.

Its like when your sitting there seeing the correct value when you hover over rowIndex and a different value over Value.ToSting() you just wanna get the valae from rowIndex and you keep trying different logical things and just cannot seem to get it out.

string keyValue = gvFans.DataKeys[row.RowIndex].Value.ToString()

You see the DataKeys is coming from an auto Incremented field and the rowIndex is really giving me the row index. SO rowIndex can be 233 and Value of the Identity field can be 5121. I do not even know why it is giving me a different value when I am asking for row.RowIndex.
 
When I do that, rows.RowIndex.ToString(); I just get some string about the class type. I cannot figure how to extract the value I can see over Row index into an int or string variable.

The answer has to be syntax and that is all.
 
Hi

Just curious... Where does the value 18 come from? Is it a value in the grid?

- DCD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top