Solved

Highlight and scroll to a certain row in datagrid

Posted on 2004-08-30
21
683 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:Sigh_Man
Comment Utility
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
Comment Utility
Yes, I think so , but give me a minute and I'll find out for sure.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
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
Comment Utility
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 55 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry for the delays by the way
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:Sigh_Man
Comment Utility
>>>  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 120 total points
Comment Utility
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
Comment Utility
ofcourse dv is the datasource for your datagrid
0
 
LVL 1

Author Comment

by:Sigh_Man
Comment Utility
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
Comment Utility
...from a DataSet, that is
0
 
LVL 1

Author Comment

by:Sigh_Man
Comment Utility
Forget the last two comments -- all sorted.

Thanks   :D
0
 
LVL 2

Expert Comment

by:chrisryhal
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I will post in that link then, although the question seems a bit different. Could post exactly what you want
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now