Solved

Searching / Sorting Datagrid

Posted on 2004-08-24
3
176 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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 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

18 Experts available now in Live!

Get 1:1 Help Now