Selecting the correct record on Gridview from inserting new record using Detailsview...

Last post 08-09-2007 3:39 PM by reiteraj. 11 replies.

Sort Posts:

  • Selecting the correct record on Gridview from inserting new record using Detailsview...

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

    Ok...I'm determined to solve this most immediate of all problems:

     I have a Gridview, I have a Detailsview: the Gridview pulls all records from a DB using an ObjectDataSource. The Details view also uses a ObjectDataSource and it pulls just one particular record based on the selected id of the Gridview. Simple straightforward...However...

    Here is my Problem: After I insert a new record using the detailsview, how the heck do I have that particular record I just inserted selected on the gridview??? I've written a routine to sort through all the pages and all the records in the gridview..but if I have over 1000 records that seems very inefficient!

    There has to be a way that I can just do something like gridview.selectedvalue = 'last inserted id retrieved from insert sql routine". Right?? Please!!

    I have no idea what I'm doing, and yet it gets done.
  • Re: Selecting the correct record on Gridview from inserting new record using Detailsview...

    02-01-2007, 5:33 AM
    • Member
      290 point Member
    • m_olia
    • Member since 09-21-2005, 6:10 AM
    • Posts 73

    well, according to DAL tutorial (find it in http://www.asp.net/learn/dataaccess/default.aspx) under "Creating Custom Insert, Update, and Delete Methods" do sth like this:

    ... 

    Code for Insert

    ... ;

    SELECT SCOPE_IDENTITY()

    this will return the id of the newly added record, for more information, take a look at tutorial .

    Hope helps ;)


     
  • Re: Selecting the correct record on Gridview from inserting new record using Detailsview...

    02-01-2007, 7:56 AM
    • Member
      148 point Member
    • Ola Karlsson
    • Member since 07-19-2006, 2:10 AM
    • Perth, Western Australia
    • Posts 39
    m_olia:

    well, according to DAL tutorial (find it in http://www.asp.net/learn/dataaccess/default.aspx) under "Creating Custom Insert, Update, and Delete Methods" do sth like this:

    ... 

    Code for Insert

    ... ;

    SELECT SCOPE_IDENTITY()

    As mentioned in the tutorial you can use SCOPE_IDENTITY for sure, however, instead of SCOPE_IDENTITY() there is a new function in SQL2005 that does exactly the same but is quite a bit easier to use, see this article http://www.dbazine.com/sql/sql-articles/cook18/ .Assuming you use SQL2005 of course ;)

    To set the selected item in the grid simply set it by specifying  

    GridView.SelectedIndex = SomeIntegerValue;

    Good luck,

    Ola

    ------------------------------------------------------------------------------------

    <sarcasm>
    Don't worry too much about marking my posts as anwers, I'm not just doing it for the points ;)</sarcasm>

    http://weblogs.asp.net/olakarlsson/
  • Re: Selecting the correct record on Gridview from inserting new record using Detailsview...

    02-01-2007, 7:56 AM
    • Member
      148 point Member
    • Ola Karlsson
    • Member since 07-19-2006, 2:10 AM
    • Perth, Western Australia
    • Posts 39
    m_olia:

    well, according to DAL tutorial (find it in http://www.asp.net/learn/dataaccess/default.aspx) under "Creating Custom Insert, Update, and Delete Methods" do sth like this:

    ... 

    Code for Insert

    ... ;

    SELECT SCOPE_IDENTITY()

    As mentioned in the tutorial you can use SCOPE_IDENTITY for sure, however, instead of SCOPE_IDENTITY() there is a new function in SQL2005 that does exactly the same but is quite a bit easier to use, see this article http://www.dbazine.com/sql/sql-articles/cook18/ .Assuming you use SQL2005 of course ;)

    To set the selected item in the grid simply set it by specifying  

    GridView.SelectedIndex = SomeIntegerValue;

    Good luck,

    Ola

    ------------------------------------------------------------------------------------

    <sarcasm>
    Don't worry too much about marking my posts as anwers, I'm not just doing it for the points ;)</sarcasm>

    http://weblogs.asp.net/olakarlsson/
  • Re: Selecting the correct record on Gridview from inserting new record using Detailsview...

    02-01-2007, 7:59 AM
    • Member
      148 point Member
    • Ola Karlsson
    • Member since 07-19-2006, 2:10 AM
    • Perth, Western Australia
    • Posts 39
    Sorry about the double post, the post button seems a bit broken, there's nothing telling you that you post have been inserted Tongue Tied
    ------------------------------------------------------------------------------------

    <sarcasm>
    Don't worry too much about marking my posts as anwers, I'm not just doing it for the points ;)</sarcasm>

    http://weblogs.asp.net/olakarlsson/
  • Re: Selecting the correct record on Gridview from inserting new record using Detailsview...

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

    First, I love you guys and your quick responses!! Big Smile

    Yeah I thought about setting the Gridview.SelectIndex = someintegervalue...however, that would mean that I would have to go through every single item in the Gridview list to compare against what I want. What if I have over 1000 records?

    That's what I mean about it seeming relatively ineffecient. I use the SELECT SCOPE_IDENTITY() on my insert routine and it returns my the last inserted ID correctly. However, what do I do with it after that? Do I actually then have to use that LastInsertID to compare against ALLLLL the records in the gridview, one by one? It seems redundant and ineffecient. I keep thinking there is something obvious I'm missing here.

     Thanks again in advance!!

    I have no idea what I'm doing, and yet it gets done.
  • Re: Selecting the correct record on Gridview from inserting new record using Detailsview...

    02-01-2007, 8:41 PM
    • Member
      148 point Member
    • Ola Karlsson
    • Member since 07-19-2006, 2:10 AM
    • Perth, Western Australia
    • Posts 39

    If you enable sorting on your GridView, you could maybe use the GriedView.Sort() function and sort it based on the id.

    The latest inserted would be the highest id and you could select that, I've never tried this but maybe worth trying or considering at least Smile

    ------------------------------------------------------------------------------------

    <sarcasm>
    Don't worry too much about marking my posts as anwers, I'm not just doing it for the points ;)</sarcasm>

    http://weblogs.asp.net/olakarlsson/
  • Re: Selecting the correct record on Gridview from inserting new record using Detailsview...

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

    Hmm, interesting suggestion. I haven't tried it yet but I'm going to give it a shot. I am thinking that it won't return the results that I would like however seeing as how I believe it's just going to sort the columns and it's not going to change the pageindex of the GridView if needed. But I'll give it a try and let you know.

    As an FYI, the below is a good example of what mine does. What I WANT it to do is when the user enters info on the DetailsView, that the record inserted will be automatically selected. I can get it to work just fine if that record is on that particular page....however what if I have over 30 pages of records??

    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: Selecting the correct record on Gridview from inserting new record using Detailsview...

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

    Yeah I tried it Olaf to no success. It only sorts the columns. Good try though!

    I have no idea what I'm doing, and yet it gets done.
  • Re: Selecting the correct record on Gridview from inserting new record using Detailsview...

    03-02-2007, 5:13 AM
    Answer
    • Member
      290 point Member
    • m_olia
    • Member since 09-21-2005, 6:10 AM
    • Posts 73

    Hi wadewalker25,

    sorry for one month delay!Big Smile 

    well, you can get  idea from "custom paging and sorting" tutorial in  Data Tutorial section.

    first determine the total number of pages, 

    >>       gridView.PageIndex =  (the_RowRank_of_currentlry_inserted_row / total_number_of_records) * PageSize ;

               gridView.SelectedIndex= (the_RowRank_of_currentlry_inserted_row / PageSize) + 1 ;

     

    the " +1 " in the latter, is because the division rounds down the resault.

     

    hope helps
     

     

  • Re: Selecting the correct record on Gridview from inserting new record using Detailsview...

    08-01-2007, 2:20 PM
    Answer
    • Member
      83 point Member
    • wadewalker25
    • Member since 05-16-2006, 1:45 AM
    • Posts 72

    I'll give that a try in the next few weeks and let you know what I come up with. This idea should work though. I've finally implemented my first custom paging on a gridview... I have no idea why I was so scared to try it. Haha. Thanks! I'm going to mark your answer as correct m_olia as I truly believe this is the best way to accomplish this...

    I have no idea what I'm doing, and yet it gets done.
  • Re: Selecting the correct record on Gridview from inserting new record using Detailsview...

    08-09-2007, 3:39 PM
    • Member
      2 point Member
    • reiteraj
    • Member since 08-09-2007, 3:20 PM
    • Houston
    • Posts 1

    I have run into this issue too. Although I found a few posts that really don't work when you use paging, I created the class below to search and return the page and grid index: (You will need to supply it with your GridView, the Primary Key, the datatable, and any filter parameters)

    Hope this helps.
    I appreciate any feedback to make it better.

    '*****Search a gridview for a key and return the PageIndex and SelectedIndex (GridIndex)
    'Supply the GridView, Key Field Value, Underlying Data Table, and any filter string
    '
    'e.g.
    'Protected Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted
    '    Dim mEquipBll As New EquipBLL
    '    Dim mGridFind As New GridFind(GridView1, e.Values("myKey"), getDataTable(), "Whatever = 'Whatever'")
    '    GridView1.PageIndex = mGridFind.PageIndex
    '    GridView1.SelectedIndex = mGridFind.GridIndex
    'End Sub

    Imports Microsoft.VisualBasic
    Imports System.Data

    Public Class GridFind

        Protected _GridView As GridView
        Protected _GridKey As Integer
        Protected _DataTable As Data.DataTable
        Protected _FilterString As String
        Protected _PageIndex As Integer
        Protected _GridIndex As Integer

        Public ReadOnly Property PageIndex() As Integer
            Get
                Return _PageIndex
            End Get
        End Property

        Public ReadOnly Property GridIndex() As Integer
            Get
                Return _GridIndex
            End Get
        End Property

        Public Sub New(ByVal fGridView As GridView, ByVal fGridKey As Integer, ByVal fDataTable As DataTable, ByVal fFilterString As String)
            _GridView = fGridView
            _GridKey = fGridKey
            _DataTable = fDataTable
            _FilterString = fFilterString

            Dim NewTable As DataTable = getNewTable()
            Dim SelectRow As Data.DataRow = NewTable.Rows.Find(_GridKey)
            Dim DataIndex As Integer = NewTable.Rows.IndexOf(SelectRow)

            _PageIndex = Fix(DataIndex / _GridView.PageSize)

            _GridIndex = DataIndex Mod _GridView.PageSize

        End Sub

        Private Function getNewTable() As DataTable

            Dim mDataView As DataView = _DataTable.DefaultView
            Dim dv As Data.DataView = _DataTable.DefaultView

            If _GridView.SortExpression <> "" Then
                dv.Sort = _GridView.SortExpression
                Select Case _GridView.SortDirection
                    Case SortDirection.Ascending
                        dv.Sort += " ASC"
                    Case SortDirection.Descending
                        dv.Sort += " DESC"
                End Select
            End If
            dv.RowFilter = _FilterString

            Dim newDt As DataTable
            newDt = dv.ToTable()
            newDt.PrimaryKey = New Data.DataColumn() {newDt.Columns(_GridView.DataKeyNames(0))}

            Return newDt

        End Function

    End Class

    Andy
Page 1 of 1 (12 items)