Solved

Highlight and scroll to a certain row in datagrid

Posted on 2004-08-30
21
690 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 55 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 120 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with LINQ and XML 10 36
send messages to whatsapp programatically 2 48
Get size of each directory on each mapped drive 5 29
Web Form VB.Net  import CSV 4 39
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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