how to keep row index after datagrid sort

Posted on 2007-10-02
Last Modified: 2013-11-26
I'm using 2005. I have an application that pulls data via data adapter to a dataset which i then use to populate text and combo boxes.  I have a datagrid on the form which is bound to the dataset.  I have set up a cell content click on the datagrid in which the selected row index value  is stored in a variable and then the text boxes are updated from the dataset using that variable because i relate the variable to the dataset row index. Point is if the datagrid is sorted by say "usernames" or whatever,  the index stays the same as the records change.  Is there a way to keep the row index of the datagrid the exact same as it was on the original population of the datagrid regardless  of whether or not a sort occurs?  

Question by:DustinHinkel
    LVL 5

    Expert Comment


    Here is the post

    Author Comment

    o.k., i think that code is c# cause i just get a syntax error.  I'm using 2005.  Anyway, i tried to do something like this.

    Private Sub DGVData_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVData.CellContentClick
            Dim cm As CurrencyManager = _
                       CType(DGVData.BindingContext(DGVData.DataSource), CurrencyManager)
            inc = cm.Position
            lblInc.Text = inc

    inc being the variable i spoke of earlier.  The subroutine navigate records uses inc to hold the value of the current row.  Here is the subroutine just so there's no confusion as to what inc is for:

    Private Sub navigaterecords()

            CBModel.Text = ds.Tables("inventory").Rows(inc).Item(1)
            TBSerial.Text = ds.Tables("inventory").Rows(inc).Item(2)
            TBAsset.Text = ds.Tables("inventory").Rows(inc).Item(3)
            TBUser.Text = ds.Tables("inventory").Rows(inc).Item(6)
            CBLocation.Text = ds.Tables("inventory").Rows(inc).Item(7)
            cbSchedule.Text = ds.Tables("Inventory").Rows(inc).Item(8)
            DTPDate.Text = ds.Tables("inventory").Rows(inc).Item(9)
            tbPrevUser.Text = ds.Tables("inventory").Rows(inc).Item(10)
            TBPart.Text = ds.Tables("inventory").Rows(inc).Item(11)
            CBEquipmenttype.Text = ds.Tables("inventory").Rows(inc).Item(12)

    I guess i don't understand currency manager all that well i though the above code would bind to the datasource and then no matter how datagrid was sorted, would give me the actual position in the dataset, but this still just gives me the position in the datagrid which is not right after sorting.  Any suggestions?
    LVL 5

    Expert Comment

    The currency Manager keeps track of the data no matter how you have it sorted.

    Look at this post.

    Author Comment

    i hear ya, so why doesn't my code work?  once sorted the currency manager position stays in the same order, but the rows change order.  am i doing something wrong.
    LVL 5

    Accepted Solution

    the datagrid is just a "view".  The currency manager is the actual data.  When you re arrange the view(dataGrid).  The currency manager will never change order.

    Here is the answer.

    Supporting posts

    Author Comment

    Hey thanks Colemss.  Your article there didn't exactly give me the functionality i wanted.  But i adapted it to work.  Just in case anybody is interested here is what i did.  Bearing in mind that item(0) is my primary key.

    Private Sub DGVData_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVData.CellContentClick

            Dim cm As CurrencyManager = CType(Me.BindingContext(DGVData.DataSource, DGVData.DataMember), CurrencyManager)
            Dim dv As DataView = CType(cm.List, DataView)
            Dim temporary As Integer
            Dim dr As DataRow
            dr = dv.Item(cm.Position).Row
            temporary = dr(0).ToString

                If temporary <> ds.Tables("inventory").Rows(inc).Item(0) Then
                    inc = 0
                    Do Until temporary = ds.Tables("inventory").Rows(inc).Item(0)
                        inc = inc + 1

                    lblInc.Text = temporary


                    MsgBox("You are clicking on the current record")

                End If
                Catch exe As Exception
            End Try

    Thanks for your help.
    LVL 5

    Expert Comment

    Glad to help

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now