[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to keep row index after datagrid sort

Posted on 2007-10-02
7
Medium Priority
?
586 Views
Last Modified: 2013-11-26
I'm using vb.net 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?  

0
Comment
Question by:DustinHinkel
  • 4
  • 3
7 Comments
 
LVL 5

Expert Comment

by:Colemss
ID: 20002855
((CurrencyManager)BindingContext[dataGrid1.DataSource]).Position;

Here is the post
http://www.thescripts.com/forum/thread418043.html
0
 

Author Comment

by:DustinHinkel
ID: 20007539
o.k., i think that code is c# cause i just get a syntax error.  I'm using vb.net 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
        navigaterecords()

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?
0
 
LVL 5

Expert Comment

by:Colemss
ID: 20007628
The currency Manager keeps track of the data no matter how you have it sorted.

Look at this post.
http://www.vb-helper.com/howto_net_bind_collection.html
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:DustinHinkel
ID: 20008950
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.
0
 
LVL 5

Accepted Solution

by:
Colemss earned 1500 total points
ID: 20009043
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.
http://support.microsoft.com/kb/817247

Supporting posts

http://msdn.microsoft.com/msdnmag/issues/03/08/AdvancedBasics/
http://support.microsoft.com/kb/317041
0
 

Author Comment

by:DustinHinkel
ID: 20013987
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
        Try

            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

                Loop
                navigaterecords()
                lblInc.Text = temporary

            Else

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


            End If
            Catch exe As Exception
            MsgBox("error")
        End Try

Thanks for your help.
0
 
LVL 5

Expert Comment

by:Colemss
ID: 20014594
Glad to help
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

825 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