gridview row command

Discussion in 'ASP .Net' started by =?Utf-8?B?Tkg=?=, Apr 18, 2006.

  1. How do I find out the row index selected in the gridveiw row command method?

    I have an imageButton in a gridview cell and its corresponding code in the
    row command method needs to know the row index in question in order to set
    the background of the cell to a different color. But the selectedIndex value
    doesnt return a value as expected.

    Is there something I am missing?
     
    =?Utf-8?B?Tkg=?=, Apr 18, 2006
    #1
    1. Advertising

  2. =?Utf-8?B?Tkg=?=

    Pipo Guest

    You can use the SelectedDataKey property to retrieve the DataKey object for
    the currently selected row. You can also use the SelectedValue property to
    retrieve the data key value for the currently selected row directly.



    "NH" <> wrote in message
    news:...
    > How do I find out the row index selected in the gridveiw row command
    > method?
    >
    > I have an imageButton in a gridview cell and its corresponding code in the
    > row command method needs to know the row index in question in order to set
    > the background of the cell to a different color. But the selectedIndex
    > value
    > doesnt return a value as expected.
    >
    > Is there something I am missing?
     
    Pipo, Apr 18, 2006
    #2
    1. Advertising

  3. thanks, but that wont let me know which row index was selected..?

    "Pipo" wrote:

    > You can use the SelectedDataKey property to retrieve the DataKey object for
    > the currently selected row. You can also use the SelectedValue property to
    > retrieve the data key value for the currently selected row directly.
    >
    >
    >
    > "NH" <> wrote in message
    > news:...
    > > How do I find out the row index selected in the gridveiw row command
    > > method?
    > >
    > > I have an imageButton in a gridview cell and its corresponding code in the
    > > row command method needs to know the row index in question in order to set
    > > the background of the cell to a different color. But the selectedIndex
    > > value
    > > doesnt return a value as expected.
    > >
    > > Is there something I am missing?

    >
    >
    >
     
    =?Utf-8?B?Tkg=?=, Apr 18, 2006
    #3
  4. =?Utf-8?B?Tkg=?=

    Pipo Guest

    not?
    Search for the row with the same id and you'll know the row, right?
    (row.FindControl("imagebutton") for the imagebutton or search for the cell
    where the imagebutton is in.


    "NH" <> wrote in message
    news:...
    > thanks, but that wont let me know which row index was selected..?
    >
    > "Pipo" wrote:
    >
    >> You can use the SelectedDataKey property to retrieve the DataKey object
    >> for
    >> the currently selected row. You can also use the SelectedValue property
    >> to
    >> retrieve the data key value for the currently selected row directly.
    >>
    >>
    >>
    >> "NH" <> wrote in message
    >> news:...
    >> > How do I find out the row index selected in the gridveiw row command
    >> > method?
    >> >
    >> > I have an imageButton in a gridview cell and its corresponding code in
    >> > the
    >> > row command method needs to know the row index in question in order to
    >> > set
    >> > the background of the cell to a different color. But the selectedIndex
    >> > value
    >> > doesnt return a value as expected.
    >> >
    >> > Is there something I am missing?

    >>
    >>
    >>
     
    Pipo, Apr 18, 2006
    #4
  5. The SelectedDataKey only holds the datakey of the datasource, this is not the
    same as the row index, the datakey could be a customerID which is not going
    to be the same as the row index. Or am I mistaken?

    "Pipo" wrote:

    > not?
    > Search for the row with the same id and you'll know the row, right?
    > (row.FindControl("imagebutton") for the imagebutton or search for the cell
    > where the imagebutton is in.
    >
    >
    > "NH" <> wrote in message
    > news:...
    > > thanks, but that wont let me know which row index was selected..?
    > >
    > > "Pipo" wrote:
    > >
    > >> You can use the SelectedDataKey property to retrieve the DataKey object
    > >> for
    > >> the currently selected row. You can also use the SelectedValue property
    > >> to
    > >> retrieve the data key value for the currently selected row directly.
    > >>
    > >>
    > >>
    > >> "NH" <> wrote in message
    > >> news:...
    > >> > How do I find out the row index selected in the gridveiw row command
    > >> > method?
    > >> >
    > >> > I have an imageButton in a gridview cell and its corresponding code in
    > >> > the
    > >> > row command method needs to know the row index in question in order to
    > >> > set
    > >> > the background of the cell to a different color. But the selectedIndex
    > >> > value
    > >> > doesnt return a value as expected.
    > >> >
    > >> > Is there something I am missing?
    > >>
    > >>
    > >>

    >
    >
    >
     
    =?Utf-8?B?Tkg=?=, Apr 18, 2006
    #5
  6. =?Utf-8?B?Tkg=?=

    Pipo Guest

    No you are right, sorry
    What I always do is that I have a column (visible = false) with the unique
    record ID.
    With that I can find the row which was selected.
    I was in the wrong assumpsion you did that also.



    "NH" <> wrote in message
    news:...
    > The SelectedDataKey only holds the datakey of the datasource, this is not
    > the
    > same as the row index, the datakey could be a customerID which is not
    > going
    > to be the same as the row index. Or am I mistaken?
    >
    > "Pipo" wrote:
    >
    >> not?
    >> Search for the row with the same id and you'll know the row, right?
    >> (row.FindControl("imagebutton") for the imagebutton or search for the
    >> cell
    >> where the imagebutton is in.
    >>
    >>
    >> "NH" <> wrote in message
    >> news:...
    >> > thanks, but that wont let me know which row index was selected..?
    >> >
    >> > "Pipo" wrote:
    >> >
    >> >> You can use the SelectedDataKey property to retrieve the DataKey
    >> >> object
    >> >> for
    >> >> the currently selected row. You can also use the SelectedValue
    >> >> property
    >> >> to
    >> >> retrieve the data key value for the currently selected row directly.
    >> >>
    >> >>
    >> >>
    >> >> "NH" <> wrote in message
    >> >> news:...
    >> >> > How do I find out the row index selected in the gridveiw row command
    >> >> > method?
    >> >> >
    >> >> > I have an imageButton in a gridview cell and its corresponding code
    >> >> > in
    >> >> > the
    >> >> > row command method needs to know the row index in question in order
    >> >> > to
    >> >> > set
    >> >> > the background of the cell to a different color. But the
    >> >> > selectedIndex
    >> >> > value
    >> >> > doesnt return a value as expected.
    >> >> >
    >> >> > Is there something I am missing?
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>
     
    Pipo, Apr 19, 2006
    #6
  7. =?Utf-8?B?Tkg=?=

    JBorders

    Joined:
    Apr 17, 2007
    Messages:
    1
    You can access the row index from within the RowCommand by using the e.CommandArgument property. "e" is the passed GridViewCommandEventArgs parameter to the RowCommand method. GridView will automatically set the CommandArgument inside this parameter to the index of the row where the command occurred (which may NOT neccessarily be the "selected" row!).

    int index = Convert.ToInt32(e.CommandArgument);

    Hope this helps.
     
    JBorders, Apr 17, 2007
    #7
  8. =?Utf-8?B?Tkg=?=

    nikitagon

    Joined:
    Dec 8, 2009
    Messages:
    1
    Reply is here blogs. ipona.com /davids/archive/2007/11/22/8447.aspx:

    <asp:TemplateField>
    <ItemTemplate>
    <asp:Button ID="IncreasePriceButton" runat="server"
    Text="+10%"
    CommandName="IncreasePrice"
    CommandArgument='<%# Container.DataItemIndex %>' />
    <asp:Button ID="DecreasePriceButton" runat="server"
    Text="-10%"
    CommandName="DecreasePrice"
    CommandArgument='<%# Container.DataItemIndex %>' />
    </ItemTemplate>
    </asp:TemplateField>
     
    nikitagon, Dec 8, 2009
    #8
  9. =?Utf-8?B?Tkg=?=

    nick5454

    Joined:
    Feb 4, 2008
    Messages:
    2

    Asp:buttonfield does the exact same thing with less markup
     
    nick5454, Mar 9, 2010
    #9
    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. Fernando Lopes
    Replies:
    0
    Views:
    3,213
    Fernando Lopes
    Apr 28, 2005
  2. misiek
    Replies:
    0
    Views:
    2,303
    misiek
    Feb 20, 2006
  3. H5N1
    Replies:
    0
    Views:
    2,768
  4. Replies:
    5
    Views:
    695
  5. D
    Replies:
    0
    Views:
    252
Loading...

Share This Page