Solved

Searching / Sorting Datagrid

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

Independent Software Vendors: 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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 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