get cell value within gridview

Discussion in 'ASP .Net' started by Mike, Oct 18, 2007.

  1. Mike

    Mike Guest

    I have a gridview that has a button in the last cell of each row. I want to get the text from cell 1 for that row the button was clicked on.

    So if my gridview looks like this:

    N-12 BMW [select]
    N-35 Mercedes [select]

    if my user clicks row 2, I want to see Mercedes, How can I do that?

    I have a buttonClick event for my select button, I'm trying to pull the values using the DataKeyName, but I keep getting 'object reference not set to an instance of an object.

    here is what i'm trying

    select_Click()
    {
    string make = grid1.SelectedDataKey.Values["CarMake"].toString();

    }

    any suggestions on how I can get the cell 1 text for the selected row within my Select_Click()?
    Mike, Oct 18, 2007
    #1
    1. Advertising

  2. Mike

    S_K Guest

    On Oct 18, 10:18 am, "Mike" <> wrote:
    > I have a gridview that has a button in the last cell of each row. I want to get the text from cell 1 for that row the button was clicked on.
    >
    > So if my gridview looks like this:
    >
    > N-12 BMW [select]
    > N-35 Mercedes [select]
    >
    > if my user clicks row 2, I want to see Mercedes, How can I do that?
    >
    > I have a buttonClick event for my select button, I'm trying to pull the values using the DataKeyName, but I keep getting 'object reference not set to an instance of an object.
    >
    > here is what i'm trying
    >
    > select_Click()
    > {
    > string make = grid1.SelectedDataKey.Values["CarMake"].toString();
    >
    > }
    >
    > any suggestions on how I can get the cell 1 text for the selected row within my Select_Click()?


    Try using the GridView.SelectedIndex value to point to the selected
    row.
    Then use the GridView1.Rows[SelectedIndex].Cells[1].Text to get the
    value inside the cell.

    I hope that helps!

    Steve
    S_K, Oct 18, 2007
    #2
    1. Advertising

  3. "Mike" <> wrote in message
    news:...

    > any suggestions on how I can get the cell 1 text for the selected row
    > within my Select_Click()?


    grid1.SelectedRow.Cells[1].Text;


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Oct 18, 2007
    #3
  4. Mike

    Mike Guest

    I tried that and it gave me this error:

    Index was out of range. Must be non-negative and less than the size
    of the collection. Parameter name: index



    "S_K" <> wrote in message
    news:...
    > On Oct 18, 10:18 am, "Mike" <> wrote:
    >> I have a gridview that has a button in the last cell of each row. I want
    >> to get the text from cell 1 for that row the button was clicked on.
    >>
    >> So if my gridview looks like this:
    >>
    >> N-12 BMW [select]
    >> N-35 Mercedes [select]
    >>
    >> if my user clicks row 2, I want to see Mercedes, How can I do that?
    >>
    >> I have a buttonClick event for my select button, I'm trying to pull the
    >> values using the DataKeyName, but I keep getting 'object reference not
    >> set to an instance of an object.
    >>
    >> here is what i'm trying
    >>
    >> select_Click()
    >> {
    >> string make = grid1.SelectedDataKey.Values["CarMake"].toString();
    >>
    >> }
    >>
    >> any suggestions on how I can get the cell 1 text for the selected row
    >> within my Select_Click()?

    >
    > Try using the GridView.SelectedIndex value to point to the selected
    > row.
    > Then use the GridView1.Rows[SelectedIndex].Cells[1].Text to get the
    > value inside the cell.
    >
    > I hope that helps!
    >
    > Steve
    >
    Mike, Oct 18, 2007
    #4
  5. Mike

    Mike Guest

    I've tried that to and get;

    Object reference not set to an instance of an object

    here is what i've tried actaully and got the above message for all
    protected void SendSales_Click(object sender, EventArgs e)
    {
    try
    {
    string carMake= " ";
    //carMake= grid1.SelectedDataKey.Values[1].ToString();
    //carMake =
    grid1.SelectedDataKey.Values["CarMake"].ToString();
    //carMake = grid1.SelectedDataKey["CarMake"].ToString();
    //carMake =
    grid1.Rows[grid1.SelectedIndex].Cells[1].Text.ToString();
    //carMake = grid1.SelectedRow.Cells[1].Text.ToString();

    Response.Write(carMake);

    }
    catch (Exception ex)
    {
    result = ex.Message;
    }
    }


    "Mark Rae [MVP]" <> wrote in message
    news:%...
    > "Mike" <> wrote in message
    > news:...
    >
    >> any suggestions on how I can get the cell 1 text for the selected row
    >> within my Select_Click()?

    >
    > grid1.SelectedRow.Cells[1].Text;
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    Mike, Oct 18, 2007
    #5
  6. Mike

    S_K Guest

    On Oct 18, 10:56 am, "Mike" <> wrote:
    > I've tried that to and get;
    >
    > Object reference not set to an instance of an object
    >
    > here is what i've tried actaully and got the above message for all
    > protected void SendSales_Click(object sender, EventArgs e)
    > {
    > try
    > {
    > string carMake= " ";
    > //carMake= grid1.SelectedDataKey.Values[1].ToString();
    > //carMake =
    > grid1.SelectedDataKey.Values["CarMake"].ToString();
    > //carMake = grid1.SelectedDataKey["CarMake"].ToString();
    > //carMake =
    > grid1.Rows[grid1.SelectedIndex].Cells[1].Text.ToString();
    > //carMake = grid1.SelectedRow.Cells[1].Text.ToString();
    >
    > Response.Write(carMake);
    >
    > }
    > catch (Exception ex)
    > {
    > result = ex.Message;
    > }
    > }
    >
    > "Mark Rae [MVP]" <> wrote in messagenews:%...
    >
    >
    >
    > > "Mike" <> wrote in message
    > >news:...

    >
    > >> any suggestions on how I can get the cell 1 text for the selected row
    > >> within my Select_Click()?

    >
    > > grid1.SelectedRow.Cells[1].Text;

    >
    > > --
    > > Mark Rae
    > > ASP.NET MVP
    > >http://www.markrae.net- Hide quoted text -

    >
    > - Show quoted text -


    If you get an error "Object instance not set..." it's because the
    GridView hasn't been created yet. Try implementing your logic in the
    GridView1_DataBound(..) event. That way you are certain that the
    GridView exists.

    Steve
    S_K, Oct 18, 2007
    #6
  7. Mike

    Mike Guest

    On this button, another developer setup an CommandArgument and is getting a
    value that way within this same SendSales_Click(), is there a way to add
    another value to that CommandArgument so it's passing 2 instead of 1 value?


    If I put this in the grid_databound method, how would that work in passing
    the value to the button when the user clicks it?
    The Grid is already bound, the values are already there in the grid, I just
    want the value from cell[1], when I click the button.



    "S_K" <> wrote in message
    news:...
    > On Oct 18, 10:56 am, "Mike" <> wrote:
    >> I've tried that to and get;
    >>
    >> Object reference not set to an instance of an object
    >>
    >> here is what i've tried actaully and got the above message for all
    >> protected void SendSales_Click(object sender, EventArgs e)
    >> {
    >> try
    >> {
    >> string carMake= " ";
    >> //carMake=
    >> grid1.SelectedDataKey.Values[1].ToString();
    >> //carMake =
    >> grid1.SelectedDataKey.Values["CarMake"].ToString();
    >> //carMake =
    >> grid1.SelectedDataKey["CarMake"].ToString();
    >> //carMake =
    >> grid1.Rows[grid1.SelectedIndex].Cells[1].Text.ToString();
    >> //carMake =
    >> grid1.SelectedRow.Cells[1].Text.ToString();
    >>
    >> Response.Write(carMake);
    >>
    >> }
    >> catch (Exception ex)
    >> {
    >> result = ex.Message;
    >> }
    >> }
    >>
    >> "Mark Rae [MVP]" <> wrote in
    >> messagenews:%...
    >>
    >>
    >>
    >> > "Mike" <> wrote in message
    >> >news:...

    >>
    >> >> any suggestions on how I can get the cell 1 text for the selected row
    >> >> within my Select_Click()?

    >>
    >> > grid1.SelectedRow.Cells[1].Text;

    >>
    >> > --
    >> > Mark Rae
    >> > ASP.NET MVP
    >> >http://www.markrae.net- Hide quoted text -

    >>
    >> - Show quoted text -

    >
    > If you get an error "Object instance not set..." it's because the
    > GridView hasn't been created yet. Try implementing your logic in the
    > GridView1_DataBound(..) event. That way you are certain that the
    > GridView exists.
    >
    > Steve
    >
    Mike, Oct 18, 2007
    #7
  8. "S_K" <> wrote in message
    news:...

    > On Oct 18, 10:56 am, "Mike" <> wrote:
    >> I've tried that to and get;
    >>
    >> Object reference not set to an instance of an object
    >>
    >> here is what i've tried actaully and got the above message for all
    >> protected void SendSales_Click(object sender, EventArgs e)
    >> {
    >> try
    >> {
    >> string carMake= " ";
    >> //carMake=
    >> grid1.SelectedDataKey.Values[1].ToString();
    >> //carMake =
    >> grid1.SelectedDataKey.Values["CarMake"].ToString();
    >> //carMake =
    >> grid1.SelectedDataKey["CarMake"].ToString();
    >> //carMake =
    >> grid1.Rows[grid1.SelectedIndex].Cells[1].Text.ToString();
    >> //carMake =
    >> grid1.SelectedRow.Cells[1].Text.ToString();
    >>
    >> Response.Write(carMake);
    >>
    >> }
    >> catch (Exception ex)
    >> {
    >> result = ex.Message;
    >> }
    >> }
    >>
    >> "Mark Rae [MVP]" <> wrote in
    >> messagenews:%...
    >>
    >>
    >>
    >> > "Mike" <> wrote in message
    >> >news:...

    >>
    >> >> any suggestions on how I can get the cell 1 text for the selected row
    >> >> within my Select_Click()?

    >>
    >> > grid1.SelectedRow.Cells[1].Text;

    >>
    >> > --
    >> > Mark Rae
    >> > ASP.NET MVP
    >> >http://www.markrae.net- Hide quoted text -

    >>
    >> - Show quoted text -

    >
    > If you get an error "Object instance not set..." it's because the
    > GridView hasn't been created yet. Try implementing your logic in the
    > GridView1_DataBound(..) event. That way you are certain that the
    > GridView exists.
    >
    > Steve
    >




    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Oct 18, 2007
    #8
  9. "S_K" <> wrote in message
    news:...

    > If you get an error "Object instance not set..." it's because the
    > GridView hasn't been created yet. Try implementing your logic in the
    > GridView1_DataBound(..) event. That way you are certain that the
    > GridView exists.


    ??? The OP is trying to get the value of one of the GridView's cells by
    clicking a button on the GridView - he wouldn't even see the button if the
    GridView hadn't been created yet...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Oct 18, 2007
    #9
  10. "Mike" <> wrote in message
    news:uhd$...

    > I've tried that to and get;
    >
    > Object reference not set to an instance of an object


    Hmm - OK...

    Set a breakpoint on the first line of the click even and run the code again

    1) Does the code jump into the correct method?

    2) Assuming it does, in the Immediate window write grid1 and press Enter -
    do you see the properties of your GridView?

    3) Assuming you do, in the Immediate window write grid1.SelectedRow and
    press Enter - what do you see...?


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Oct 18, 2007
    #10
  11. Mike

    Mike Guest

    1) Does the code jump into the correct method?
    Yes

    2) Assuming it does, in the Immediate window write grid1 and press Enter -
    do you see the properties of your GridView;
    yes, but these properties are set to the following, even though I
    selected a row via the button
    SelectedDataKey: null
    SelectedIndex: -1
    SelectedRow: null
    SelectedRowStyle: {System.Web.UI.WebControls.TableItemStyle}
    SelectedValue: null


    3) Assuming you do, in the Immediate window write grid1.SelectedRow and
    press Enter - what do you see...? null is displayed in the windows



    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "Mike" <> wrote in message
    > news:uhd$...
    >
    >> I've tried that to and get;
    >>
    >> Object reference not set to an instance of an object

    >
    > Hmm - OK...
    >
    > Set a breakpoint on the first line of the click even and run the code
    > again
    >
    > 1) Does the code jump into the correct method?
    >
    > 2) Assuming it does, in the Immediate window write grid1 and press Enter -
    > do you see the properties of your GridView?
    >
    > 3) Assuming you do, in the Immediate window write grid1.SelectedRow and
    > press Enter - what do you see...?
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    Mike, Oct 18, 2007
    #11
  12. Mike

    Ian Semmel Guest

    Just put a test

    if ( SelectedIndex < 0 )
    return;



    > -----Original Message-----
    > From: Mike [mailto:]
    > Posted At: Friday, 19 October 2007 3:49 AM
    > Posted To: microsoft.public.dotnet.framework.aspnet
    > Conversation: get cell value within gridview
    > Subject: Re: get cell value within gridview
    >
    > 1) Does the code jump into the correct method?
    > Yes
    >
    > 2) Assuming it does, in the Immediate window write grid1 and press
    > Enter -
    > do you see the properties of your GridView;
    > yes, but these properties are set to the following, even

    though
    > I
    > selected a row via the button
    > SelectedDataKey: null
    > SelectedIndex: -1
    > SelectedRow: null
    > SelectedRowStyle:
    > {System.Web.UI.WebControls.TableItemStyle}
    > SelectedValue: null
    >
    >
    > 3) Assuming you do, in the Immediate window write grid1.SelectedRow
    > and
    > press Enter - what do you see...? null is displayed in the windows
    >
    >
    >
    > "Mark Rae [MVP]" <> wrote in message
    > news:...
    > > "Mike" <> wrote in message
    > > news:uhd$...
    > >
    > >> I've tried that to and get;
    > >>
    > >> Object reference not set to an instance of an object

    > >
    > > Hmm - OK...
    > >
    > > Set a breakpoint on the first line of the click even and run the

    code
    > > again
    > >
    > > 1) Does the code jump into the correct method?
    > >
    > > 2) Assuming it does, in the Immediate window write grid1 and press

    > Enter -
    > > do you see the properties of your GridView?
    > >
    > > 3) Assuming you do, in the Immediate window write grid1.SelectedRow

    > and
    > > press Enter - what do you see...?
    > >
    > >
    > > --
    > > Mark Rae
    > > ASP.NET MVP
    > > http://www.markrae.net
    Ian Semmel, Oct 18, 2007
    #12
  13. "Mike" <> wrote in message
    news:...

    >> 1) Does the code jump into the correct method?

    > Yes


    OK.

    >> 2) Assuming it does, in the Immediate window write grid1 and press
    >> Enter - do you see the properties of your GridView?

    > SelectedRow: null


    Aha - well there's your problem right there...

    Whatever you are doing behind the click of the Buttons in the GridView's
    rows, it's not actually marking the row the Button is on as Selected...

    Fix that, and everything else should work...

    Alternately, wire up a SelectedIndexChanged event to the GridView

    protected void grid1_SelectedIndexChanged(Object sender, EventArgs e)
    {
    GridViewRow objRow = grid1.SelectedRow;
    }

    Does that at least tell you which row is selected...?


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Oct 18, 2007
    #13
  14. "Ian Semmel" <> wrote in message
    news:A8A047B34A504EA5A7DE7F646DBB42AE@DIMITY...

    > Just put a test
    >
    > if ( SelectedIndex < 0 )
    > return;


    How will that help...?


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Oct 18, 2007
    #14
  15. Mike

    Mike Guest

    I've just noticed (after about an hour on this), the developer that created
    this page is using a template field that has a imgButton in it. So he has
    the image Button calling the Sales_Click() method. he is currently passing
    the ID of the field using the CommandArgument.

    So I guess I have a new issue then with this. Is there a way to either pass
    2 parameters in the command argument or is there a way to get the value from
    a particular cell within the grid when the user clicks the imgButton on a
    row?




    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "Mike" <> wrote in message
    > news:...
    >
    >>> 1) Does the code jump into the correct method?

    >> Yes

    >
    > OK.
    >
    >>> 2) Assuming it does, in the Immediate window write grid1 and press
    >>> Enter - do you see the properties of your GridView?

    >> SelectedRow: null

    >
    > Aha - well there's your problem right there...
    >
    > Whatever you are doing behind the click of the Buttons in the GridView's
    > rows, it's not actually marking the row the Button is on as Selected...
    >
    > Fix that, and everything else should work...
    >
    > Alternately, wire up a SelectedIndexChanged event to the GridView
    >
    > protected void grid1_SelectedIndexChanged(Object sender, EventArgs e)
    > {
    > GridViewRow objRow = grid1.SelectedRow;
    > }
    >
    > Does that at least tell you which row is selected...?
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    Mike, Oct 18, 2007
    #15
    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. G. Whiz

    Gridview: Get the value in the cell

    G. Whiz, Mar 1, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,939
    Ken Cox - Microsoft MVP
    Mar 1, 2006
  2. Chris

    Get GridView Cell Value

    Chris, Apr 5, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    76,325
    imranwahabit
    Jun 14, 2009
  3. Phillip Roncoroni
    Replies:
    14
    Views:
    1,086
    Toby A Inkster
    Apr 5, 2004
  4. graphicsxp

    get value of a cell in a gridview

    graphicsxp, May 22, 2006, in forum: ASP .Net
    Replies:
    12
    Views:
    88,461
    brianguy16
    Feb 20, 2008
  5. Dave Hewes

    How to get Gridview cell value on select?

    Dave Hewes, Feb 28, 2007, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    226
    Dave Hewes
    Feb 28, 2007
Loading...

Share This Page