Link to home
Start Free TrialLog in
Avatar of Sigh_Man
Sigh_Man

asked on

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...
Avatar of RonaldBiemans
RonaldBiemans

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
Avatar of Sigh_Man

ASKER

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.
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
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!?!)
Yes, I think so , but give me a minute and I'll find out for sure.
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
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.
SOLUTION
Avatar of iboutchkine
iboutchkine

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!!)
Sorry for the delays by the way
>>>  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..."
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ofcourse dv is the datasource for your datagrid
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
...from a DataSet, that is
Forget the last two comments -- all sorted.

Thanks   :D
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?
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.
No problem, I actually have done so already here:

https://www.experts-exchange.com/questions/21284699/Search-datagrid-question.html

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

CR
Moderators:

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

CR
I will post in that link then, although the question seems a bit different. Could post exactly what you want