Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search row in datagrid

Status
Not open for further replies.

160473

IS-IT--Management
Apr 16, 2003
55
0
0
FI
I have a datagrid binded to a dataset and it shows data ok.
Now i need a search function...
I have created a textbox where the users can write their search string. I need a function that hilights the row in the datagrid corresponding to the value they just entered in the textbox.
 
This might not be the best way, but the idea is that you find the data you are looking for using the item(row,col) index and then scroll to the row using a nice little extended class and then call the select method of the datagrid..

In a project with a form add

1 datagrid called datagrid1
1 textbox called textbox1
1 commandbutton (you get the idea)

In the code that follows I have made the inherited class blue to make it easier to see the code that does the scrolling to the row..

Add the following code..
Code:
    Dim ds As New DataSet
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable("Sample Rows")
        dt.Columns.Add("RowID", GetType(System.Int16)
        Dim dr As DataRow

        For i As Integer = 0 To 100
            dr = dt.NewRow
            dr("AlarmID") = i
            dt.Rows.Add(dr)
        Next
        ds.Tables.Add(dt)

        DataGrid1.DataSource = ds.Tables(0).DefaultView

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
            If DataGrid1.Item(i, 0).ToString = TextBox1.Text Then
                Me.DataGrid1.Select(i)
                Me.DataGrid1.ScrollToRow(i)
                Exit Sub
            End If
        Next

    End Sub
End Class
[blue]
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
[/blue]

Then edit the "Designer added code" to modify any reference to the system.forms.datagrid to be MyDataGrid..

e.g.

Code:
    Friend WithEvents DataGrid1 As [blue]MyDataGrid[/blue]

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.TextBox1 = New System.Windows.Forms.TextBox

        Me.DataGrid1 = New [blue]MyDataGrid[/blue]
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()

Then when you bring up the form, type a valid row number in the textbox and it will go to that row...


HTH


Rob

PS credit for the inherited class needs to go to George Shepherd (syncfusion) and Daniel Herling @ MS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top