datagrid的頁內查找函數

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

在自己學著開發的過程中,寫了一些可重用的代碼,願與大家分享一下.

因為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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航