Gridview - Detailsivew: How do I get the Gridview to select the last inserted record from the detailsview?

Last post 08-02-2008 4:16 PM by BHendry. 11 replies.

Sort Posts:

  • Gridview - Detailsivew: How do I get the Gridview to select the last inserted record from the detailsview?

    02-01-2007, 7:34 PM
    • Member
      83 point Member
    • wadewalker25
    • Member since 05-16-2006, 1:45 AM
    • Posts 72

    How do I make the gridview go to a particular record without using the SelectedIndex?

    I have a GridView and DetailsView both on one page. I use Selection on the GridView to pull data from a particular table to populate data on the DetailsView. No problem thus far. However, when I insert a record using the DetailsView and click insert the GridView doesn't automatically select the last record inserted. How do I get that to work? How do I make the Gridview select the last inserted record from the DetailsView?

    Now, I know about using SELECT SCOPE_IDENTITY(); I've built an insert routine to return the last inserted ID. But how do I use that information (the last inserted ID) to make the Gridview select that record?

     This is killing me!! I've been searching for a week for a solution to this problem and nothing so far. This has had to have been explored right!??

    Thank you all in advance!

    I have no idea what I'm doing, and yet it gets done.
  • Re: Gridview - Detailsivew: How do I get the Gridview to select the last inserted record from the detailsview?

    02-01-2007, 8:39 PM
    • Member
      83 point Member
    • wadewalker25
    • Member since 05-16-2006, 1:45 AM
    • Posts 72
    I have no idea what I'm doing, and yet it gets done.
  • Re: Gridview - Detailsivew: How do I get the Gridview to select the last inserted record from the detailsview?

    02-01-2007, 9:02 PM
    • Participant
      1,236 point Participant
    • Rafa
    • Member since 04-22-2005, 3:52 PM
    • Ladera Ranch, CA
    • Posts 250

    Here is one way you could do it

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

     

    string ident = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "CommentID"));

    if (ident == "your scope identity")

    Session[

    "Index"] = Convert.ToInt16(e.Row.RowIndex);

     

     

     

    }

    protected void GridView1_DataBound(object sender, EventArgs e)

    {

    if (Session["Index"] != null)

    GridView1.SelectedIndex =

    Convert.ToInt16(Session["Index"]);

    }

    http://incendy.spaces.live.com
  • Re: Gridview - Detailsivew: How do I get the Gridview to select the last inserted record from the detailsview?

    02-02-2007, 3:26 AM

    Hi, wadewalker25: 

    you can make your insert sql command something like this:

    InsertCommand=”INSERT INTO [Names] ([name]) VALUES (@name);SELECT @NewID = Scope_Identity()”

    Then, have in your codebehind, this:

     protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)

        {

            object newId = (integer)  e.Command.Parameters[“@NewId”].Value;

          

        }

     

    Then, you will get your inserted it.

    Best Regards,
    __________________________________________________
    Sincerely,
    Rex Lin
    Microsoft Online Community Support

    If there is any question or the issue is not resolved, please feel free to mark the thread as not resolved
  • Re: Gridview - Detailsivew: How do I get the Gridview to select the last inserted record from the detailsview?

    02-02-2007, 8:59 AM
    • Member
      83 point Member
    • wadewalker25
    • Member since 05-16-2006, 1:45 AM
    • Posts 72

    Hi Rafa, thanks for the quick reply! Smile

     I tried what you said, I translated to VB as the following:

    Protected Sub DSDemoInfo_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles DSDemoInfo.Inserted
       Session(
    "decRetID") = e.ReturnValue
    End Sub

    Protected Sub Grid1AllDemo_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles Grid1AllDemo.DataBound
       If Session("RowIndex") <> Nothing Then
         
    Grid1AllDemo.SelectedIndex = Convert.ToInt16(Session("RowIndex"))
       End If
    End Sub

    Protected Sub Grid1AllDemo_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Grid1AllDemo.RowDataBound
      
    Dim ident As String = CStr(DataBinder.Eval(e.Row.DataItem, "ClientID"))
      
    If ident <> Nothing And Session("RowIndex") <> Nothing Then
         
    If ident = Session("decRetID") Then
            
    Session("RowIndex") = Convert.ToInt16(e.Row.RowIndex)
         
    End If
      
    End If
    End Sub

    It ALMOST does what I want. This code searches through each row as it's bound to the gridview and evaluates whether or not the ID matches my return ID, which is good. However...what it doesn't do is search through each page. I have paging enabled on the GridView. How do I also get this to search through all the pages and THEN search through all the rows, stopping once a match is found?

    Thanks very much Rafa for your help!! I would hug you if I could!! :)

    I have no idea what I'm doing, and yet it gets done.
  • Re: Gridview - Detailsivew: How do I get the Gridview to select the last inserted record from the detailsview?

    02-02-2007, 9:07 AM
    • Member
      83 point Member
    • wadewalker25
    • Member since 05-16-2006, 1:45 AM
    • Posts 72

    Thanks Rex.

    Well, I am able to return the ID of the last inserted record. What I need to be able to do is then use that return ID effectively. I.E. I need to take that return ID and then compare it against ALL the data in my Gridview and automatically selecting that record which matches. Below is a good example of what mine is currently doing. I can insert a record on the DetailsView but it's not automatically selecting that record back on the GridView. Any Suggestions?

    http://quickstarts.asp.net/QuickStartv20/aspnet/samples/data/GridViewMasterDetailsInsert_vb.aspx

    I have no idea what I'm doing, and yet it gets done.
  • Re: Gridview - Detailsivew: How do I get the Gridview to select the last inserted record from the detailsview?

    02-03-2007, 2:41 AM
    • Member
      83 point Member
    • wadewalker25
    • Member since 05-16-2006, 1:45 AM
    • Posts 72

    Guys, I just had a thought...

    What if I could use an sql statement to select based on a specific ID (the ID that was last inserted), somehow counting the index slot it's in and apply or associate that to the gridview? I have no idea how to do this however Sad. I'll start looking...any ideas?

    I have no idea what I'm doing, and yet it gets done.
  • Re: Gridview - Detailsivew: How do I get the Gridview to select the last inserted record from the detailsview?

    02-19-2007, 12:59 AM
    • Member
      83 point Member
    • wadewalker25
    • Member since 05-16-2006, 1:45 AM
    • Posts 72
    Any Ideas?? Anyone?
    I have no idea what I'm doing, and yet it gets done.
  • Re: Gridview - Detailsivew: How do I get the Gridview to select the last inserted record from the detailsview?

    02-27-2007, 6:49 AM
    • Member
      2 point Member
    • sushmim
    • Member since 02-27-2007, 11:44 AM
    • Posts 1

    Hi Wade :)

     Wonder if you got a solution to your problem. But I guess all you need to do is keep the PageIndex in the session just like you kept the RowIndex. Then do the following

     Considering that your Pagesize is 10 - 10 records per page:-

    Protected Sub Grid1AllDemo_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles Grid1AllDemo.DataBound
       If (Session("RowIndex") <> Nothing)And(Session("PageIndex") <> Nothing) Then
         
    Grid1AllDemo.SelectedIndex = (Session("PageIndex") *10) + Convert.ToInt16(Session("RowIndex"))
       End If
    End Sub
     

    I think this should solve your problem.

    Let me know..  

  • Re: Gridview - Detailsivew: How do I get the Gridview to select the last inserted record from the detailsview?

    01-02-2008, 2:39 AM

    I too have the same problem.. i am using paging in the grid page size 10, i have the record in page 4 and 8 record, but the RowDataBound event of the grid runs only through the 10 records of the current page index. How to move to the next page in the RowDataBound event..

    any ideas WADE.. did you get the solution for this?

    Thanks  

     

     

    Prashant
  • Re: Gridview - Detailsivew: How do I get the Gridview to select the last inserted record from the detailsview?

    01-02-2008, 9:55 AM
    • Member
      83 point Member
    • wadewalker25
    • Member since 05-16-2006, 1:45 AM
    • Posts 72

    Hi Kumar, no unfortunately I have not been able to find a credible solution to this. However, here is a suggestion I received (along with the ones above): Someone suggested that you make the gridview display all the records initially (paging off), and then in the page load event, you search for the index of the record you are looking for and once you have it, turn paging back on the gridview and go to the page/record your looking for. They said that it happened so fast that the end user wouldn't even notice it. I don't know, i haven't tried it. Good luck, let me know if you find something else out.

     p.s. say hi to harold for me! :)

    I have no idea what I'm doing, and yet it gets done.
  • Re: Gridview - Detailsivew: How do I get the Gridview to select the last inserted record from the detailsview?

    08-02-2008, 4:16 PM
    • Participant
      1,644 point Participant
    • BHendry
    • Member since 01-25-2006, 11:48 AM
    • Posts 296

    I found this post looking for the same solution; selecting the last inserted item from a formview/detailsview on a paged gridview.

    I was not successful in finding a working example out there, so I thought I'd post my solution for what it's worth. It's working correctly. I welcome any improvements/suggestions from more experienced members.

    Problem: Using the GridView.SelectedIndex method to dynamically select a row will not work if the sought after row is in a different page of the GridView.

    Solution: Iterate through each page of the GridView until we find the row and then use SelectIndex to focus the user on that row.

    Assumptions: As noted in the post above, this assumes we already have the SelectedIndex value we want to select. In this case, a recently inserted identity value added by way of a DetailsView wired to a GridView. We're also assuming the page sizes and total records for the GridView are of a manageable number; if the result set is huge than I wouldn't use this solution. I would instead default new entries to land on the last page of the GridView and use the SelectedIndex on that last page, or limit the results to contain only the newly added row, or a small subset of the data that includes the row.

    The following example is in VB. It uses the GridView.DataBound event and iterates through the GridView rows collection on every page looking for the SelectedIndex that matches the DataKey we are seeking. If found, the SelectedIndex is set and the process exits. If value is never found, the GridView is set back to its first page before exiting.

        Protected Sub gvwGalleryAdmin_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwGalleryAdmin.DataBound
    
            '_newGalleryID will have a value only when the DetailsView has just completed an insert
            'if there is no value, we don't want to do anything
            If _newGalleryID > 0 Then
    
                'page through grid until we find the row to select
                Dim isSelected As Boolean = False
    
                'keep trying until we find a match or hit the last page/row of GridView
                Do While isSelected = False
                    'use SelectGridViewRow method to search for match
                    If SelectGridviewRow(gvwGalleryAdmin,CStr(_newGalleryID)) = True Then
                        'we found our row, reset _newGalleryID
                        _newGalleryID = 0
                        'get out
                        Exit Sub
                    Else
                        'item not found on this page, advance to next or exit if last page
                        If gvwGalleryAdmin.PageIndex < gvwGalleryAdmin.PageCount - 1 Then
                            'advance to next page
                            gvwGalleryAdmin.PageIndex = gvwGalleryAdmin.PageIndex + 1
                            'we exit here because the page change will cause the DataBound event to fire again
                            'and the next page will be searched
                            Exit Sub
                        Else
                            'did not find row to select in every page, set back to 1st page and exit
                            gvwGalleryAdmin.PageIndex = 0
                            Exit Do
                        End If
                    End If
                Loop
    
            End If
            
        End Sub
    
        Private Function SelectGridviewRow(ByRef thisGridview As GridView, ByVal dataKeyValue As String) As Boolean
    
            'iterate through thisGridview looking for dataKeyValue, return True if found
            Dim hasItem As Boolean = False
    
            With thisGridview
                For i As Integer = 0 to .Rows.Count - 1
                    'search the DataKey for our value
                    If .DataKeys(i).Value = dataKeyValue Then
                        'we found a match, set the SelectedIndex, return True and get out
                        .SelectedIndex = i
                        hasItem = True
                        Exit For
                    End If
                Next
            End With
    
            'if match was not found, hasItem is False
            Return hasItem
    
        End Function
     
    Bruce


    Please remember to click "Mark as Answer" on the posts that helped solve your issue.
Page 1 of 1 (12 items)