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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select a GridView Row by Value Not by Index 3

Status
Not open for further replies.

bangkokbill

Programmer
May 19, 2008
5
US
Is it possible to select a gridview row by value instead of setting selectedindex? I know selectedvalue is readonly, but maybe there's another way aside from iterating through all the rows and checking keys

The reason i need to select value instead of index is that after updating record its index in the grid could easily change depending on the sort expression and the newly updated values.
 
that's what i would do if looking for an item my key
Code:
foreach(GridViewRow row in GridView.Rows)
{
   if(row.DataKeys["Id"] == myId)
   {
       GridView.SelectedIndex = row.RowIndex;
       break;
   }
}
or something to that effect.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I was afraid so. I simply want the row highlighted to indicate to the users which row was updated so it's immediately apparent to them. I guess I can expect performance issues for larger record sets if the record in question is at the bottom of the list.

I just can't understand why it's possible to set SelectedValue for dropdownlists and radiobuttonlists, but not for gridviews. Oh well.
 
how do you know there will be a preformance hit? how have to determined that enumerating the gridview is where the preformance hit lies?

as a general rule IO where you take the biggest preformance hit. Database connections and File operations. the next preformance hit I notice with asp.net is usually rendering with viewstate. none of these issues directly deal with iterating a collection.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I probably don't know what I'm talking regarding performance issues.

However, now that I'm looking at this option of looping through the records, I can't even find a datakeys property on the GridViewRow. Can you provide exact syntax, since the closesg i can find is "row.RowIndex" which takes me back to where I started?
 
Code:
foreach(GridViewRow row in MyGridView.Rows)
{
   if(MyGridView.DataKeys[row.RowIndex]["Id"] == myId)
   {
       GridView.SelectedIndex = row.RowIndex;
       break;
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
My brain's not working today. That was too obvious!

Thanks for taking the time to help me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top