GridView & DataKeyNames

R

Robert Warnestam

I've recently switched from using a DataGrid component to a GridView and now
run in to some problems.

My ambition is easy; first show a list of users in a GridView component and
for each row in the grid have a SelectButton on it.

At the bottom I show how I'm trying to do it in the GridView (and with some
minor changes the code functioned with the older component DataGrid). The
first time I see the page I can see my users, but any attempt to choose a
user resolves in the following error message "Index was outside the bounds
of the array."
I seems to me that the keys dissapear and maybe one solution could be that
recreate the list of users each time the page is loaded - but that is a
solution I'm not interested in.
As standard the SelectCommand returns the row index of the row that was
selected. Could this been changed so it returns a unique id instead?

My quesitons is;

Q1) Is it possible to use the GridView in the same way I did with DataGrid?
Q2) Is it possible to use DataKey together with my own class as the
DataSource?
Q3) How can I use a CommandButton to create my own SelectCommand

Thanks

Robert Warnestam


void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Get a list of all users. A user is an instant of the class TapUser
TapUser[] users = TapUser.GetAllUsers();
// Set the datasource and the primary key
// The class TapUser has a property called UserID, this will function as our
key
gvUser.DataSource = users;
string[] keys = new string[1] {"UserID"};
// Bind the grid
gvUser.DataBind();
gvUser.SelectedIndex = 0;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top