How can I find the datakey of currently selected row?

N

needin4mation

I searched for this and found an answer, but it did not help. In my
GridView there is a fileupload and a button click event to save the
file upload. I am naming the file uploaded after the primary key field
of my table, so, if I have employeeid 4, then my file name is 4.jpg.

What I cannot do is reference that current employeeid. If click edit,
my edittemplate show and my fileupload and button are visible. In that
buttons event, I need the employee id of that row.

I'm not sure how to get that.

Thank you for any help.
 
M

Mark E. Hansen

I searched for this and found an answer, but it did not help. In my
GridView there is a fileupload and a button click event to save the
file upload. I am naming the file uploaded after the primary key field
of my table, so, if I have employeeid 4, then my file name is 4.jpg.

What I cannot do is reference that current employeeid. If click edit,
my edittemplate show and my fileupload and button are visible. In that
buttons event, I need the employee id of that row.

I'm not sure how to get that.

Thank you for any help.

If you have the current selected index (GridView1.SelectedIndex) you can
do this:

GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;

Where '1' is the 0-based index of the grid view cell that contains your
column.

There may be a way to access the desired column by column name, but
I'm new to ASP.NET and haven't found a way to do that yet. If anyone
knows how, I would sure like to know ;-)
 
D

Daniel Yafac

Hi from Lima - Perú

Dim D1 As DataKey, CadSQL As String

D1 = GridView1.SelectedDataKey

CadSQL = D1("employeeid")

good luck
 
N

needin4mation

I actually tried this yesterday before my post, but I keep getting an
object reference error:

DataKey d1 = null;
string empID = null;

d1 = GridView1.SelectedDataKey;
empID = Convert.ToString(d1["EmployeeID"]);

Response.Write("Empid: " + empID);

EmployeeID is indeed one of the datakeynames in the grid. I cannot
figure out why I get

Object reference not set to an instance of an object.

Thanks for any help.
 
N

needin4mation

I searched for this and found an answer, but it did not help. In my
GridView there is a fileupload and a button click event to save the
file upload. I am naming the file uploaded after the primary key field
of my table, so, if I have employeeid 4, then my file name is 4.jpg.

What I cannot do is reference that current employeeid. If click edit,
my edittemplate show and my fileupload and button are visible. In that
buttons event, I need the employee id of that row.

I'm not sure how to get that.

Thank you for any help.

After much searching here is what I did:

if (e.CommandName == "addImage")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row =
(GridViewRow)((Control)e.CommandSource).Parent.Parent;
FileUpload fileUpload = (FileUpload)
GridView1.Rows[row.RowIndex].FindControl("FileUpload1");

if (fileUpload.HasFile)
{
fileUpload.SaveAs(Server.MapPath(".\\images\\") +
e.CommandArgument + ".jpg");//get employeeid
}
else
{
Response.Write("No File Uploaded.");
}



}

Where e.CommandArgument is assigned when the control is built by
CommandArgument='<%# Eval("employeeid")%>'

The commandargument holds the data for my file and the commandsource
parent(s) hold the row value so that I know what row clicked the upload
button.
 

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
474,001
Messages
2,570,254
Members
46,849
Latest member
Fira

Latest Threads

Top