How can I find the datakey of currently selected row?

Discussion in 'ASP .Net Web Controls' started by needin4mation@gmail.com, Oct 16, 2006.

  1. Guest

    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.
    , Oct 16, 2006
    #1
    1. Advertising

  2. On 10/16/06 14:32, 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 ;-)
    Mark E. Hansen, Oct 16, 2006
    #2
    1. Advertising

  3. Daniel Yafac Guest

    Hi from Lima - Perú

    Dim D1 As DataKey, CadSQL As String

    D1 = GridView1.SelectedDataKey

    CadSQL = D1("employeeid")

    good luck

    <> escribió en el mensaje
    news:...
    >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.
    >
    Daniel Yafac, Oct 16, 2006
    #3
  4. Guest

    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:
    > Hi from Lima - Perú
    >
    > Dim D1 As DataKey, CadSQL As String
    >
    > D1 = GridView1.SelectedDataKey
    >
    > CadSQL = D1("employeeid")
    >
    > good luck
    >
    > <> escribió en el mensaje
    > news:...
    > >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.
    > >
    , Oct 17, 2006
    #4
  5. Guest

    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.
    , Oct 17, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGljaw==?=

    Persisting the currently selected row on a GridView

    =?Utf-8?B?RGljaw==?=, Jan 19, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    4,416
    Steven Cheng[MSFT]
    Jan 23, 2006
  2. Replies:
    3
    Views:
    272
    Kevin Walzer
    Apr 21, 2007
  3. JeffBuzz

    GridView Select Row By DataKey

    JeffBuzz, Mar 2, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    6,169
    Michael Nemtsev [MVP]
    Mar 3, 2008
  4. jc
    Replies:
    1
    Views:
    1,176
    Teresita Núñez - SorCereSs -
    Mar 6, 2008
  5. D
    Replies:
    0
    Views:
    207
Loading...

Share This Page