I have a GridView control bound to an ObjectDataSource which returns a list of objects that look like this:
public class X {
public string DisplayName {
get;
}
public string Guid {
get;
}
}
I don’t want to show the Guid property in the GridView, but I need to retrieve it when an object is selected.
I could not find a “DataValueField” property, like for the ListBox control.
As a workaround, I tried to set Visible=false for the column bound to the Guid property and get the cell text from the Row object in the SelectedIndexChanged method:
protected void GridView1_SelectedIndexChanged(object sender,EventArgs e){
GridViewRow row = GridView1.SelectedRow;
string id = row.Cells[2].Text; // empty
}
But this does not work – apparently if the column is not visible its Text property is left empty.
How can I get the Guid of the selected object while showing only the DisplayName?
Not sure about how this works with ObjectDataSource, but with SqlDataSource we set keys on the rows of the GridView.
Then, you can get the key by doing this: