Solved

Searching / Sorting Datagrid

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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