I have added a GridView control on my ASP.net webpage and data bound it to a List<> the list contains a collection of a simple custom objects which is defined as:
public class PersonRecord
{
public int PersonId { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Notes { get; set; }
}
I have set AutoGenerateSelectButton to true and and attached an event handler to the SelectedIndexChanged event. I can see my event handler fires and I can get the selected row index by using MyGridView.SelectedIndex.
My question is: how do I use the selected row index to get the PersonId for the selected record?
I thought MyGridView.Rows[MyGridView.SelectedIndex].Cells[0] would do it but it doesn’t because MyGridView.Rows.Count is 0.
TIA
How are you storing the data from your GridView on the server (session, viewstate, or are you not doing so?). Since you have the selected row index, you just need to get your datasource again. If you persist it in session, then you can just get that session object and get find the object at the index the user selected.