Home All Groups Group Topic Archive Search About

How can I find the datakey of currently selected row?

Author
16 Oct 2006 9:32 PM
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.

Author
16 Oct 2006 9:44 PM
Mark E. Hansen
On 10/16/06 14:32, needin4mat***@gmail.com wrote:
> 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 ;-)
Author
16 Oct 2006 10:50 PM
Daniel Yafac
Hi from Lima - Perú

Dim D1 As DataKey, CadSQL As String

D1 = GridView1.SelectedDataKey

CadSQL =  D1("employeeid")

good luck

<needin4mat***@gmail.com> escribió en el mensaje
Show quoteHide quote
news:1161034346.346368.149840@k70g2000cwa.googlegroups.com...
>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.
>
Author
17 Oct 2006 1:38 PM
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.

Daniel Yafac wrote:
Show quoteHide quote
> Hi from Lima - Perú
>
> Dim D1 As DataKey, CadSQL As String
>
> D1 = GridView1.SelectedDataKey
>
> CadSQL =  D1("employeeid")
>
> good luck
>
> <needin4mat***@gmail.com> escribió en el mensaje
> news:1161034346.346368.149840@k70g2000cwa.googlegroups.com...
> >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.
> >
Author
17 Oct 2006 8:41 PM
needin4mation
needin4mat***@gmail.com wrote:
> 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.