Get GridView row index

Discussion in 'ASP .Net' started by DavidC, Feb 26, 2010.

  1. DavidC

    DavidC Guest

    I have the code below that runs when a textbox in an ItemTemplate changes. My
    problem is that it always updates the 1st row. How can I change the value in
    DataKeys to reflect the index of the row I am on?

    p.s. I am using AutoPostback and UpdatePanel. Thanks.


    Protected Sub txtWeek1Units_TextChanged(ByVal sender As Object, ByVal e
    As System.EventArgs)
    Dim txBox As TextBox = CType(sender, TextBox)
    Dim intTempID As Int32 =
    Convert.ToInt32(gvTimesheetEntry.DataKeys(0).Value)
    Dim dblUnits As Double = 0
    If txBox.Text <> "" Then
    dblUnits = Convert.ToDouble(txBox.Text)
    End If
    Dim lbl As Label = LblMsg
    If PayrollClass.UpdateTimesheetsTemp(intTempID, dblUnits, 1) = False
    Then
    lbl.CssClass = "Show"
    lbl.Text = "Update of temporary timesheet entry failed for Week1
    Units"
    Else
    lbl.CssClass = "Hide"
    lbl.Text = ""
    End If
    End Sub
    --
    David
    DavidC, Feb 26, 2010
    #1
    1. Advertising

  2. DavidC

    Mr. Arnold Guest

    DavidC wrote:
    > I have the code below that runs when a textbox in an ItemTemplate changes. My
    > problem is that it always updates the 1st row. How can I change the value in
    > DataKeys to reflect the index of the row I am on?
    >
    > p.s. I am using AutoPostback and UpdatePanel. Thanks.
    >



    Here is a ItemTemplete but it's a RadListView with ItemTemplete.


    protected void RadListView_ItemCommand(object sender,
    RadListViewCommandEventArgs e)
    {
    if (e.CommandName == RadListView.UpdateCommandName)
    {
    var groupID =
    (int)((RadListViewDataItem)e.ListViewItem).GetDataKeyValue("GroupID");
    //rest of the code
    }
    }

    It's going to be some e.Item that's going to point you to the DataKey of
    the Grid row selected.


    I also think you can get the datakey at the OnSelectedIndexChanged on
    the gridview.

    var key = gridview1.selected.value with postback.

    You would save it to do hidden field and retive the key from hidden
    field for later usage.


    You can also catch it at ItemBound doing something with the e.Item like
    it's being done with ItemCommand()above.
    Mr. Arnold, Feb 27, 2010
    #2
    1. Advertising

  3. DavidC

    Andy O'Neill Guest

    "Mr. Arnold" <> wrote in message
    news:%...
    > DavidC wrote:
    >> I have the code below that runs when a textbox in an ItemTemplate
    >> changes. My problem is that it always updates the 1st row. How can I
    >> change the value in DataKeys to reflect the index of the row I am on?
    >>
    >> p.s. I am using AutoPostback and UpdatePanel. Thanks.
    >>

    >
    >
    > Here is a ItemTemplete but it's a RadListView with ItemTemplete.
    >
    >
    > protected void RadListView_ItemCommand(object sender,
    > RadListViewCommandEventArgs e)
    > {
    > if (e.CommandName == RadListView.UpdateCommandName)
    > {
    > var groupID =
    > (int)((RadListViewDataItem)e.ListViewItem).GetDataKeyValue("GroupID");
    > //rest of the code
    > }
    > }
    >
    > It's going to be some e.Item that's going to point you to the DataKey of
    > the Grid row selected.
    >
    >
    > I also think you can get the datakey at the OnSelectedIndexChanged on the
    > gridview.
    >
    > var key = gridview1.selected.value with postback.
    >
    > You would save it to do hidden field and retive the key from hidden field
    > for later usage.
    >
    >
    > You can also catch it at ItemBound doing something with the e.Item like
    > it's being done with ItemCommand()above.
    >
    >


    In your xhtml for your gridview you need datakeynames="fieldname"
    That gives you a collection of DataKeys
    So in csharp
    DataKey dk = gv.DataKeys[index]
    int MyId =(int)dk.Values["fieldname"]

    Personally, I would have a button or something in my row.
    I reckon the simplest way is to have an explicit row command.
    Then something like

    protected void gv_RowCommand(object sender, GridviewCommandArguments e)
    {
    if e.commandbame.equals("thebuttoncommand"))
    {
    int index = convert.toint32(e.commandargument);



    Then the above code with the datakeys to give you the id of your record,
    pass that into your object/datalayer/whatever.
    Andy O'Neill, Feb 27, 2010
    #3
  4. DavidC

    DavidC Guest

    I tried the SelectedIndexChanged event but I do not want to have to "select"
    the row before doing anything. I just want the user to enter data into the
    textbox, update the underlying database record (based on the DataKey value)
    and then move down to the next row (via user tabbing to it) and do the same
    thing with multiple rows. Picture a list of rows where each row is updated
    as data is entered in the textbox. Does this change anything in your
    recommendation? I am not sure the ItemCommand fires each time the cursor
    moves down to the new row. Thanks.
    --
    David


    "Mr. Arnold" wrote:

    > DavidC wrote:
    > > I have the code below that runs when a textbox in an ItemTemplate changes. My
    > > problem is that it always updates the 1st row. How can I change the value in
    > > DataKeys to reflect the index of the row I am on?
    > >
    > > p.s. I am using AutoPostback and UpdatePanel. Thanks.
    > >

    >
    >
    > Here is a ItemTemplete but it's a RadListView with ItemTemplete.
    >
    >
    > protected void RadListView_ItemCommand(object sender,
    > RadListViewCommandEventArgs e)
    > {
    > if (e.CommandName == RadListView.UpdateCommandName)
    > {
    > var groupID =
    > (int)((RadListViewDataItem)e.ListViewItem).GetDataKeyValue("GroupID");
    > //rest of the code
    > }
    > }
    >
    > It's going to be some e.Item that's going to point you to the DataKey of
    > the Grid row selected.
    >
    >
    > I also think you can get the datakey at the OnSelectedIndexChanged on
    > the gridview.
    >
    > var key = gridview1.selected.value with postback.
    >
    > You would save it to do hidden field and retive the key from hidden
    > field for later usage.
    >
    >
    > You can also catch it at ItemBound doing something with the e.Item like
    > it's being done with ItemCommand()above.
    >
    >
    > .
    >
    DavidC, Feb 27, 2010
    #4
  5. DavidC

    Mr. Arnold Guest

    DavidC wrote:
    > I tried the SelectedIndexChanged event but I do not want to have to "select"
    > the row before doing anything. I just want the user to enter data into the
    > textbox, update the underlying database record (based on the DataKey value)
    > and then move down to the next row (via user tabbing to it) and do the same
    > thing with multiple rows. Picture a list of rows where each row is updated
    > as data is entered in the textbox. Does this change anything in your
    > recommendation? I am not sure the ItemCommand fires each time the cursor
    > moves down to the new row. Thanks.


    Then you need a Edit Template with a asp:Edit button with property
    Command="Edit". The Edit template can have this asp:textbox as a control.

    Then you're going to need a Update Template with an asp:Button
    Text="Update" Command="Update" and an asp:Button Text="Cancel"
    Command="Cancel".

    And it's still on the GrivView_ItemCommand(object sender, EventArgs e)
    like the example previously provided to you but a 3rd party
    <telrick:RadListView> control.

    It's the same thing, which is off of e.Item as the item in the grid you
    are working on.

    protected void RadListView_ItemCommand(object sender,
    RadListViewCommandEventArgs e)
    {
    if (e.CommandName == "Update")
    {
    var groupID =
    (int)((RadListViewDataItem)e.ListViewItem).GetDataKeyValue("GroupID");

    //rest of the code -- You can run off to the database and do it.
    }
    }
    Mr. Arnold, Feb 27, 2010
    #5
  6. DavidC

    DavidC Guest

    Given this solution, does the user have to click the edit button on every row?
    --
    David


    "Mr. Arnold" wrote:

    > DavidC wrote:
    > > I tried the SelectedIndexChanged event but I do not want to have to "select"
    > > the row before doing anything. I just want the user to enter data into the
    > > textbox, update the underlying database record (based on the DataKey value)
    > > and then move down to the next row (via user tabbing to it) and do the same
    > > thing with multiple rows. Picture a list of rows where each row is updated
    > > as data is entered in the textbox. Does this change anything in your
    > > recommendation? I am not sure the ItemCommand fires each time the cursor
    > > moves down to the new row. Thanks.

    >
    > Then you need a Edit Template with a asp:Edit button with property
    > Command="Edit". The Edit template can have this asp:textbox as a control.
    >
    > Then you're going to need a Update Template with an asp:Button
    > Text="Update" Command="Update" and an asp:Button Text="Cancel"
    > Command="Cancel".
    >
    > And it's still on the GrivView_ItemCommand(object sender, EventArgs e)
    > like the example previously provided to you but a 3rd party
    > <telrick:RadListView> control.
    >
    > It's the same thing, which is off of e.Item as the item in the grid you
    > are working on.
    >
    > protected void RadListView_ItemCommand(object sender,
    > RadListViewCommandEventArgs e)
    > {
    > if (e.CommandName == "Update")
    > {
    > var groupID =
    > (int)((RadListViewDataItem)e.ListViewItem).GetDataKeyValue("GroupID");
    >
    > //rest of the code -- You can run off to the database and do it.
    > }
    > }
    > .
    >
    DavidC, Feb 27, 2010
    #6
  7. DavidC

    Mr. Arnold Guest

    DavidC wrote:
    > Given this solution, does the user have to click the edit button on every row?


    Yeah, if the user wants to edit the textbox in the templete.

    The edit button is going to be in the template for every row on the grid.
    Mr. Arnold, Feb 27, 2010
    #7
  8. DavidC

    DavidC Guest

    Below is what I am doing on a different web page that updates any row in an
    ItemTemplate when the user checks a checkbox in that row and it works
    perfectly. That page uses a ListView but the concept should be the same. The
    user does not need to click on an edit button, it just grabs the key and runs
    an update. I am hoping this helps you show me how to do the same thing in a
    GridView on a TextChanged event. Thanks.

    Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)
    ' Gets the CheckBox object that fired the event.
    Dim chkBox As CheckBox = CType(sender, CheckBox)

    ' Gets the item that contains the CheckBox object.
    Dim item As ListViewDataItem = CType(chkBox.Parent, ListViewDataItem)
    Dim intTransID As Int32 =
    Convert.ToInt32(lvIncExpTrans.DataKeys(item.DataItemIndex).Value)

    If VendorClass.ChangeIncExpTransPick(intTransID, chkBox.Checked) =
    False Then
    Dim tb As TextBox = Page.Master.FindControl("txtMsg")
    ' Replace ListView1 with ID of your ListView Control
    'tb.Text = "TransID sent = " &
    lvIncExpTrans.DataKeys(item.DataItemIndex).Value
    tb.Text = "The update to the checkbox failed."
    End If
    lvIncExpTrans.DataBind()
    End Sub

    --
    David


    "Mr. Arnold" wrote:

    > DavidC wrote:
    > > Given this solution, does the user have to click the edit button on every row?

    >
    > Yeah, if the user wants to edit the textbox in the templete.
    >
    > The edit button is going to be in the template for every row on the grid.
    > .
    >
    DavidC, Feb 28, 2010
    #8
  9. DavidC

    DavidC Guest

    FYI, I solved it by putting the datakey value into a hidden column on the row
    and used the code below. Now I just need to use javascript to loop through
    the GridView rows to total the values the user enters into each TextBox. I
    have that code in my TextChanged event (see previous post) and it works great
    except that using FindControl screws up the tab order and puts the cursor in
    the footer. Have you found any code examples that loop through a GridView
    (table) and grabs values of specific column controls (td's)? Thanks.

    Dim lbl As Label = CType(sender.parent.FindControl("LblTempID"),
    Label)
    Dim intTempID As Int32 = Convert.ToInt32(lbl.Text)


    --
    David


    "Mr. Arnold" wrote:

    > DavidC wrote:
    > > Given this solution, does the user have to click the edit button on every row?

    >
    > Yeah, if the user wants to edit the textbox in the templete.
    >
    > The edit button is going to be in the template for every row on the grid.
    > .
    >
    DavidC, Feb 28, 2010
    #9
  10. DavidC

    Mr. Arnold Guest

    DavidC wrote:
    > FYI, I solved it by putting the datakey value into a hidden column on the row
    > and used the code below. Now I just need to use javascript to loop through
    > the GridView rows to total the values the user enters into each TextBox. I
    > have that code in my TextChanged event (see previous post) and it works great
    > except that using FindControl screws up the tab order and puts the cursor in
    > the footer. Have you found any code examples that loop through a GridView
    > (table) and grabs values of specific column controls (td's)? Thanks.
    >
    > Dim lbl As Label = CType(sender.parent.FindControl("LblTempID"),
    > Label)
    > Dim intTempID As Int32 = Convert.ToInt32(lbl.Text)
    >
    >


    No, I have not had to loop through TD's in javascript. Where I would
    might try to catch it is on ItemBound event off of e.item again and
    FindControl that would be the item the user was working on at the time.
    Mr. Arnold, Feb 28, 2010
    #10
    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. H5N1
    Replies:
    0
    Views:
    2,697
  2. Replies:
    5
    Views:
    645
  3. troutbum

    Cannot Find Row Index after using row filter

    troutbum, Jan 22, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    10
    Views:
    459
    Andre
    Feb 19, 2004
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    266
    Tomasz Chmielewski
    Mar 4, 2008
  5. Giggle Girl

    ie6 Insert Row at specific Row Index of Table

    Giggle Girl, Feb 5, 2006, in forum: Javascript
    Replies:
    18
    Views:
    254
    Thomas 'PointedEars' Lahn
    Feb 7, 2006
Loading...

Share This Page