?
Solved

Highlight and scroll to a certain row in datagrid

Posted on 2004-08-30
21
Medium Priority
?
703 Views
Last Modified: 2008-02-01
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...
0
Comment
Question by:Sigh_Man
  • 9
  • 8
  • 3
  • +1
21 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11930258
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
 
LVL 1

Author Comment

by:Sigh_Man
ID: 11930353
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11930463
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:Sigh_Man
ID: 11930508
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11930548
Yes, I think so , but give me a minute and I'll find out for sure.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11930665
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11931072
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
 
LVL 28

Assisted Solution

by:iboutchkine
iboutchkine earned 220 total points
ID: 11933635
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
 
LVL 1

Author Comment

by:Sigh_Man
ID: 11959954
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
 
LVL 1

Author Comment

by:Sigh_Man
ID: 11959963
Sorry for the delays by the way
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 11959980
>>>  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
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 480 total points
ID: 11960888
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11960889
ofcourse dv is the datasource for your datagrid
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 11978965
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
 
LVL 1

Author Comment

by:Sigh_Man
ID: 11978990
...from a DataSet, that is
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 11982342
Forget the last two comments -- all sorted.

Thanks   :D
0
 
LVL 2

Expert Comment

by:chrisryhal
ID: 13132914
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13133335
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
 
LVL 2

Expert Comment

by:chrisryhal
ID: 13133482
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
 
LVL 2

Expert Comment

by:chrisryhal
ID: 13133488
Moderators:

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

CR
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13133539
I will post in that link then, although the question seems a bit different. Could post exactly what you want
0

Featured Post

Industry Leaders: 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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 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