新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:datagrid的頁內查找函數

datagrid的頁內查找函數

新客网 XKER.COM 2004-09-28 来源: 收藏本文
在自己學著開發的過程中,寫了一些可重用的代碼,願與大家分享一下.
因為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


收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐