Highlight and scroll to a certain row in datagrid

I am writing a WinForms application in VB.Net with MSDE.

I have a form, which contains a datagrid and a textbox.  By default, the datagrid Rows are sorted by the client's surname.  What I want to do is to take the contents of the textbox and use this to select and scroll to the corresponding value in the 'Surname' column of the datagrid.

Eg.  User types in "Smith" in the textbox, and immediately (on textbox1.TextChanged, say) the datagrid scrolls to the first "Smith".

Does anyone have any sample code for this?

Thanks in advance...
LVL 1
Sigh_ManAsked:
Who is Participating?
 
RonaldBiemansConnect With a Mentor Commented:
Hi Sigh_man,

Wouldn't it be simpler just to do this than

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        dv.RowFilter = "Surname LIKE '" & TextBox1.Text & "%'"

    End Sub
0
 
RonaldBiemansCommented:
Hi Sigh_Man,


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

if this case a user has to press a button.
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
 
Sigh_ManAuthor Commented:
Ronald,

Thanks for that.  Before I tuck into your code, would your answer differ significantly if I said "no, I'm not using a DataView"...?  I am currently linking directly to the dataset created at RunTime.  Of course, I can use DV if I have to...

Thanks.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RonaldBiemansCommented:
Hi Sigh_Man,

Well a bit, the find method doesn't work directly on a table in a dataset (only for the primary key), the nice thing about the find method of a dataview is that it returns the recordnumber which you than can use for the select method of the datagrid
0
 
Sigh_ManAuthor Commented:
So, will using DataView in this solution allow me to cater for instances where a user clicks on other column headers, thereby sorting the datagrid in a way other than the way in which it was first displayed?  Eg. user clicks on First Name and therefore the datagrid now shows the data, but sorted by First Name?

(I hope that made sense!?!)
0
 
RonaldBiemansCommented:
Yes, I think so , but give me a minute and I'll find out for sure.
0
 
RonaldBiemansCommented:
But just keep using your current setup and create the dataview only to get the recordnumber, like

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       dim dv as new dataview = yourdataset.tables(0).defaultview
        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
 
RonaldBiemansCommented:
Aha, I think I understand what you mean now (I'm a bit slow).

Do you mean the grid is sorted on firstname but you want to search on lastname but you want to keep the sorting on firstname. If so then you have to loop through all the records yourself to find the record you want.
0
 
iboutchkineConnect With a Mentor Commented:
Scroll to a specific row in a System.Windows.Forms.Datagrid


The following examples show how to set and get the current cell.

If the current cell is set to a cell which is not currently visible, the datagrid will scroll to that cell.

Sample Code:

Private Sub SetCellWithFocus(ByVal myGrid As DataGrid)
' Set the current cell to cell 1, row 1.
myGrid.CurrentCell = New DataGridCell(1,1)
End Sub

Private Sub DataGrid1_GotFocus(ByVal Sender As Object, ByVal e As EventArgs)
Console.WriteLine(DataGrid1.CurrentCell.ColumnNumber & " " & _
DataGrid1.CurrentCell.RowNumber)
End Sub

=================================
'scroll to the new added row
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim dr As DataRow = ds.Tables(0).NewRow
        dr(0) = "Iouri"
        ds.Tables(0).Rows.Add(dr)
        dg.CurrentCell = New DataGridCell(ds.Tables(0).Rows.Count - 1, 1)' (row,cell)
    End Sub
0
 
Sigh_ManAuthor Commented:
Just to give this some perspective, this is all going in my "NewClient" form, which will generate a new client number.

I wanted to remain sorted by Surname, then as the user types

"S" - datagrid scrolls to first row containing surnames beginning with S
  "M" - datagrid scrolls to first row containing surname beginning with SM
I
T
H etc

Then, a txtbox creates a new client code, using the first four letters, ie "SMIT", then adding "0001" or "0002" - whichever is the next available code.  So if there are 7 Smiths already in the database, the next code created would be "SMIT0008".  I know how to create the logic to make this happen, but I wanted the database to scroll and select the surname as the user types each letter in a txtbox, ie on .TextChanged.  So the sorting in the datagrid won't have to change.  However, my concern was if a user clicked on a column header, thereby manually sorting the dg - would this upset the scrolling/selection of the rows.  IN FACT - I've realised - if the user sorts by another column, then maybe I should disable sorting in this screen (at the risk of frustrating the users!!)
0
 
Sigh_ManAuthor Commented:
Sorry for the delays by the way
0
 
Sigh_ManAuthor Commented:
>>>  Then, a txtbox creates a new client code, using ...

This should read:  "The contents of the textbox are then used to createa new client code, using..."
0
 
RonaldBiemansCommented:
ofcourse dv is the datasource for your datagrid
0
 
Sigh_ManAuthor Commented:
I've used various bits of the above and managed to get a suitable solution.

One last thing:  I know this is gonna be easy for you guys -- what is the code to look to see whether a certain column (in this case, column 'ClientID') contains a certain string?

Thanks
0
 
Sigh_ManAuthor Commented:
...from a DataSet, that is
0
 
Sigh_ManAuthor Commented:
Forget the last two comments -- all sorted.

Thanks   :D
0
 
chrisryhalCommented:
RonaldBiemens:

I have been strugling with this same thing.  I used your method (The Accepted Answer Here) and nothing happens.  Here is how I used it:

  Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        dv.RowFilter = "ToolID LIKE '" & txtStartAt.Text & "%'"

    End Sub

No error, just nothing happens.  Can you help out?
0
 
RonaldBiemansCommented:
Sorry Chrisryhal, but by EE rules it is not allowed to use somebody else his question to ask your own.
so you have to ask a new question yourself,maybe with a link to this one.
0
 
chrisryhalCommented:
No problem, I actually have done so already here:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21284699.html

I am having a horrible problem performing my question.  Hope to see you on that topic.

CR
0
 
chrisryhalCommented:
Moderators:

Sorry, I was unaware about RonaldBiemans posting, with the EE Guidelines.  A question was previously open

CR
0
 
RonaldBiemansCommented:
I will post in that link then, although the question seems a bit different. Could post exactly what you want
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.