datagrid的頁內查找函數

类别:.NET开发 点击:0 评论:0 推荐:

在自己學著開發的過程中,寫了一些可重用的代碼,願與大家分享一下.
因為datagrid沒有像delphi中的網格的網格內數據的查找功能,于是自己動手寫了一個:


'
Public Function findto(ByVal mDataGrid As DataGrid, ByVal nowindex As Integer, ByVal maxindex As Integer, ByVal caps As Boolean, ByVal updown As Boolean, ByVal mFieldName As Integer, ByVal mFieldValue As String) As Integer

        Dim rowIndex As Integer = -1
        Dim regstring As String
        regstring = mFieldValue.Replace("\", "\\")
        Dim dgrow As Integer
        Try
            Dim rx As System.Text.RegularExpressions.Regex
            If caps Then
                rx = New System.Text.RegularExpressions.Regex(regstring, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
            Else
                rx = New System.Text.RegularExpressions.Regex(regstring)
            End If
            If updown Then
                For dgrow = nowindex - 1 To 0 Step -1
                    If rx.IsMatch(mDataGrid.Item(dgrow, mFieldName).ToString) Then
                        rowIndex = dgrow
                        If rowIndex <= nowindex Then
                            mDataGrid.CurrentRowIndex = rowIndex
                        Else
                            rowIndex = -1
                        End If
                        Exit For
                    End If
                Next
            Else
                For dgrow = nowindex + 1 To maxindex
                    If rx.IsMatch(mDataGrid.Item(dgrow, mFieldName).ToString) Then
                        rowIndex = dgrow
                        If rowIndex >= nowindex Then
                            mDataGrid.CurrentRowIndex = rowIndex
                        Else
                            rowIndex = -1
                        End If
                        Exit For
                    End If
                Next
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Return rowIndex

    End Function

'使用時
        Dim findindex As Integer '找到的行
        Dim currow As Integer
        Dim curcol As Integer
        curcol = sFieldName.SelectedIndex
        If curcol = 0 Then Exit Sub
        currow = DataGrid1.CurrentRowIndex '
        DataGrid1.UnSelect(currow)
       '功能說明:  (選中該行)
       'findto(網格的名稱,當前行號,數據記錄數,區分大小寫,向上or向下查找方向,列號,查詢值)
        findindex = findto(DataGrid1, currow, ds_rec.Tables("rec").Rows.Count - 1, cap, searup, curcol, sFieldValue.Text.ToString)
        If findindex <> -1 Then
            DataGrid1.Select(findindex)
        Else
            MsgBox("沒有找到匹配的項目!", MsgBoxStyle.Information, "查找結果")
            DataGrid1.CurrentRowIndex = currow
            DataGrid1.Select(currow)
        End If


本文地址:http://com.8s8s.com/it/it43966.htm