DataGrid ASP.NET 2.0 C#

  • Thread starter Mike Gleason jr Couturier
  • Start date
M

Mike Gleason jr Couturier

Hi,

I display a list of users (my SELECT contains the name and the ID of the
users) in a DataGrid.

The users display fine (single column).
On the SelectedIndexChanged event, I want to retreive the user ID.

Is there a way to attach the user ID on a particular row ?

I already tried to display the ID on a second column and get the ID by doing
:
GridViewUsers.SelectedRow.Cells[1].Text
But I don't want the ID to be displayed... I'm looking for other
alternatives.

Thanks!
 
M

Mike Gleason jr Couturier

Mike Gleason jr Couturier said:
Hi,

I display a list of users (my SELECT contains the name and the ID of the
users) in a DataGrid.

The users display fine (single column).
On the SelectedIndexChanged event, I want to retreive the user ID.

Is there a way to attach the user ID on a particular row ?

I already tried to display the ID on a second column and get the ID by
doing :
GridViewUsers.SelectedRow.Cells[1].Text
But I don't want the ID to be displayed... I'm looking for other
alternatives.

Thanks!


I finally found a way but I wonder if it's "the" good way :

protected void GridViewUsers_SelectedIndexChanged(object sender,
EventArgs e)
{
int userID =
(int)GridViewUsers.DataKeys[GridViewUsers.SelectedIndex].Value;
[...]
}

Thanks !!
 
P

Phil H

Hi Mike

Glad you asked about this. I've been confronted with this sort of
problem too recently.

Your solution:

int userID =
int)GridViewUsers.DataKeys[GridViewUsers.SelectedIndex].Value;

could be a bit more direct with:

int userID = (int) GridViewUsers.SelectedValue;

HTH

message



I display a list of users (my SELECT contains the name and the ID of the
users) in a DataGrid.
The users display fine (single column).
On the SelectedIndexChanged event, I want to retreive the user ID.
Is there a way to attach the user ID on a particular row ?
I already tried to display the ID on a second column and get the ID by
doing :
GridViewUsers.SelectedRow.Cells[1].Text
But I don't want the ID to be displayed... I'm looking for other
alternatives.

I finally found a way but I wonder if it's "the" good way :

protected void GridViewUsers_SelectedIndexChanged(object sender,
EventArgs e)
{
int userID =
(int)GridViewUsers.DataKeys[GridViewUsers.SelectedIndex].Value;
[...]
}

Thanks !!- Hide quoted text -

- Show quoted text -
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top