Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Searching / Sorting Datagrid

Posted on 2004-08-24
3
Medium Priority
?
189 Views
Last Modified: 2010-04-23
Hi I am working on a program which uses a datagrid.  I created a search function that just refills the dataset with a new SQL command (SELECT * FROM TABLE WHERE......) but the people I am making it for would like it so when you search it shows all the records on the datagrid and just scrolls down and highlights the first record found. How could you do this?
0
Comment
Question by:shadowarts
  • 2
3 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11882592
Hi shadowarts,

try something like this (I assume in this example the following, you use a dataview as your datasource for the grid (dv)
and the column you want to search is numeric



if it is a string value

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dv.Sort = "Yourcolumn"
        DataGrid1.Select(dv.Find("yoursearchstring"))   '<-- find the row that has the string yoursearchstring in column yourcolumn
    End Sub

this will only select the row not scroll to it, if you want to scroll to have to create your own datagrid like

 
     Public Class MyDataGrid
 
      Inherits DataGrid
 
          Sub ScrollToRow(ByVal row As Integer)
 
               If Not Me.DataSource Is Nothing Then
 
                    Me.GridVScrolled(Me, New ScrollEventArgs(ScrollEventType.LargeIncrement, row))
 
               End If
 
          End Sub

     End Class
 
and call like this

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dv.Sort = "Yourcolumn"
        DataGrid1.Select(dv.Find(8))   '<-- find the row that has a value 8 in column yourcolumn
       datagrid1.ScrollToRow(DataGrid1.CurrentRowIndex())
    End Sub
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 2000 total points
ID: 11882729
sorry something went wrong there

Hi shadowarts,

try something like this (I assume in this example the following, you use a dataview as your datasource for the grid (dv)

if the column is numeric

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dv.Sort = "Yourcolumn"
        DataGrid1.Select(dv.Find(8))   '<-- find the row that has a value 8 in column yourcolumn
    End Sub


if it is a string value

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dv.Sort = "Yourcolumn"
        DataGrid1.Select(dv.Find("yoursearchstring"))   '<-- find the row that has the string yoursearchstring in column yourcolumn
    End Sub

this will only select the row not scroll to it, if you want to scroll to have to create your own datagrid like

 
     Public Class MyDataGrid
 
      Inherits DataGrid
 
          Sub ScrollToRow(ByVal row As Integer)
 
               If Not Me.DataSource Is Nothing Then
 
                    Me.GridVScrolled(Me, New ScrollEventArgs(ScrollEventType.LargeIncrement, row))
 
               End If
 
          End Sub

     End Class
 
and call like this

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dv.Sort = "Yourcolumn"
        DataGrid1.Select(dv.Find(8))   '<-- find the row that has a value 8 in column yourcolumn
       datagrid1.ScrollToRow(DataGrid1.CurrentRowIndex())
    End Sub
0
 

Author Comment

by:shadowarts
ID: 11893015
That works very well, I wasn't using a DataView but its easy enough to add, though in the end I found anoother way to search without the DataView (with a for loop).  The find method seems picky about datatypes and I didn't have to time to mess with it.  But the Scroll Function is awesome.  I wouldn't think it would be so easy to do that. Thanks very much! By the way here is my search code:

For I As Integer = 0 To myDataSet.Tables("Inbound").Rows.Count - 1
             If myDataSet.Tables("Inbound").Rows(I)(myParse(0)) = myParse(1) Then
                    Index = I
                End If
            Next I

            If Index = myDataSet.Tables("Inbound").Rows.Count - 1 Or Index = 0 Then
                MessageBox.Show("No records found matching your request", "Notice")
                Exit Sub
            End If
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

783 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