Solved

Perform Find using Dataview in Datagridview without using a For Loop

Posted on 2009-06-27
7
277 Views
Last Modified: 2012-05-07
I have a form that contains a dataviewgrid that can contain a large number of rows.  The sort on the dataview is preset and can also be modified by a pre-defined set of sorts in a dropdown.

The thing I would like to be able to incorporate into this form is a text box where the user can enter a value associated with the FIRST FIELD in the sort order.  At the same time if the sort order contains multiple fields, can I perform a find as I've included common code below to locate the first occurance of the first field in the sort?  I specifically need to be able to move the pointer to this row (glCurrentRow) so that a double-click will allow me to get an ID field hidden in the dataviewgrid.

Dim i As Int64 = 0, vals(1) As Object

        vals(0) = Convert.ToInt64(txtIndex.Text)

        vals(1) = glInvoiceAssessment

        i = dvGlobalDuesInvoices.Find(vals)

        If i >= 0 Then

            glCurrentInvoiceAssessmentEntryRow = i

            glCurrentRow = i

        Else

            glCurrentInvoiceAssessmentEntryRow = 0

            glCurrentRow = 0

        End If

Open in new window

0
Comment
Question by:TSFLLC
  • 4
  • 3
7 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 24728616
If you are using a DataView, why don't you filter the information using RowFilter() ?
http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx 
0
 

Author Comment

by:TSFLLC
ID: 24729061
I can't because the code above populates the glCurrentRow variable which is used with MoveNext / MovePrevious subs on the Entry form.  This variable is critical.

The user is able to click a Browse button on the entry form which opens a form with a datagridview using the same datatable.  However, the user is able to change the sort order with a dropdown that may have 3-4 option sort orders.  Then the user double-clicks on the row (activating and repopulating the Entry form) and because of the MoveNext/Previous buttons on the entry form, I must be able to maintain a current row with the COMPLETE datatable.

But the main issue is wanting to just stop at the first instance of a value in the first field in a sort.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24729341
You want to maintain the selected row after the sort ? Is that it ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 48

Expert Comment

by:jpaulino
ID: 24729348
Check this workaround:
Option Strict On

 

Public Class Form1

 

    Private currentRowID As Integer

 

    ' looks for the index of the ID

    Function getDGindex(ByVal ID As Integer) As Integer

        Dim dr As DataGridViewRow

        For Each dr In DataGridView1.Rows

            If CType(dr.Cells(0).Value, Integer) = ID Then

                Return dr.Index

            End If

        Next

    End Function

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim rnd As New Random

        Dim dt As New DataTable

        dt.Columns.Add("ID", GetType(Byte))

        dt.Columns.Add("Description", GetType(String))

        dt.Columns.Add("Random", GetType(Integer))

 

        Dim dr As DataRow

        For x As Byte = 0 To 100

            dr = dt.NewRow

            dr("ID") = x

            dr("Description") = "Item" & x.ToString.PadLeft(3, "0"c)

            dr("Random") = rnd.Next(0, 1000)

            dt.Rows.Add(dr)

        Next

 

        Me.DataGridView1.DataSource = dt

        Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

    End Sub

 

    ' On row leave detects the select ID and stores it on currentRowID 

    Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave

        If e.RowIndex <> -1 And Not Me.DataGridView1.Rows(e.RowIndex).IsNewRow Then

            currentRowID = CType(Me.DataGridView1(0, e.RowIndex).Value, Integer)

        End If

    End Sub

 

    ' After sort is selects again the selected item

    Private Sub DataGridView1_Sorted(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Sorted

        Dim index As Integer = getDGindex(currentRowID)

        Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(index).Cells(0)

    End Sub

 

End Class

Open in new window

0
 

Author Comment

by:TSFLLC
ID: 24729370
>> You want to maintain the selected row after the sort ? Is that it ?
Yes.

As I specified in the Title.....Without a For Loop
I can have upwards of 40-50 thousand records in this datagridview.  Haven't tested it but I would think using a Loop with this many rows would be pathetic in terms of speed/efficiency.  But I'll check it out just to see.
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 24729380
You can loop in the DataSource instead, using the same code but for the DataSource, that is quicker.
0
 

Author Closing Comment

by:TSFLLC
ID: 31597581

Prefer not to loop but this does work appropriately.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to open a link from vb.net app 3 23
Hide Tab Page 3 34
Format column on datatable 7 29
Unable  to create new object 9 41
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 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

11 Experts available now in Live!

Get 1:1 Help Now