从datagridview中的选定行滚动 [英] Scroll from selected row in datagridview

查看:195
本文介绍了从datagridview中的选定行滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个代码,其中在datagridview中搜索项目,当用户点击enter或down键时焦点设置为datagridview,我希望从所选行开始滚动,但它从最后选择的行开始



 私人 功能 FindItems( ByVal  strSearchString  As   String  As   Boolean  
dgvsearchitem.ClearSelection()
对于 每个 myRow 作为 DataGridViewRow dgvsearchitem.Rows
对于 每个 myCell As DataGridViewCell myRow.Cells
If InStr(myCell.Value.ToString,strSearchString,CompareMethod.Text)然后
myRow.Selected = True
返回 True
结束 如果
下一步
下一步
返回 错误

结束 功能
私有 Sub txtitemsearch_KeyDown( ByVal 发​​件人作为 对象 BYVAL e As System.Windows.Forms.KeyEventArgs)句柄 txtitemsearch.KeyDown
如果(e.KeyCode = Keys.Enter e.KeyCode = Keys.Down e.KeyCode = Keys.Tab) txtiname.Text<> 然后
dgvsearchitem .Focus()

结束 如果

结束 Sub

解决方案

这段代码对我有用..

我有自己的DataGridView实现,我的数据绑定到BindingSource _BS ..

也许你可以给它一枪。







 公共 功能 FindValue( ByVal  tcValue  字符串可选  ByVal  tcColumn 作为 字符串 =   as  布尔 
如果 String .IsNullOrEmpty(tcValue.Trim)然后
Dim lcColumn 作为 字符串 = .CurrentCell .OwningColumn.DataPropertyName
如果 String .IsNullOrEmpty(tcColumn)然后
lcColumn = tcColumn
结束 < span class =code-keyword>如果
如果 字符串 .IsNullOrEmpty (lcColumn)然后
lcColumn = .CurrentCell.OwningColumn.Name
End 如果
对于 每个 loRow 作为 DataGridViewRow .Rows
If loRow.Cells(lcColumn).EditedFormattedValue.ToString.ToUpper()。StartsWith(tcValue.ToUpper())然后
._ BS.Position = loRow.Index
返回 True
结束 如果
下一步
结束 如果
返回 错误
结束 功能


如果问题仍未解决:



该用法的正确语法是:

 dgvsearchitem.rows(yourIndex).Selected =  True < /跨度> 


i created a code where an item is searched in the datagridview and when a user hits enter or down key the focus is set to the datagridview , I want the scroll start from the selected row but it is starting from the last selected row

 Private Function FindItems(ByVal strSearchString As String) As Boolean
    dgvsearchitem.ClearSelection()
 For Each myRow As DataGridViewRow In dgvsearchitem.Rows
        For Each myCell As DataGridViewCell In myRow.Cells
            If InStr(myCell.Value.ToString, strSearchString,CompareMethod.Text) Then
                myRow.Selected = True
                Return True
            End If
        Next
    Next
    Return False

End Function
 Private Sub txtitemsearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtitemsearch.KeyDown
    If (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Down Or e.KeyCode = Keys.Tab) And txtiname.Text <> "" Then
        dgvsearchitem.Focus()

    End If

End Sub

解决方案

This code works for me..
I have an own implementation of DataGridView and my data is bound to a BindingSource _BS..
Maybe you can give it a shot.



Public Function FindValue(ByVal tcValue As String, Optional ByVal tcColumn As String = "") As Boolean
   If Not String.IsNullOrEmpty(tcValue.Trim) Then
      Dim lcColumn As String = Me.CurrentCell.OwningColumn.DataPropertyName
      If Not String.IsNullOrEmpty(tcColumn) Then
         lcColumn = tcColumn
      End If
      If String.IsNullOrEmpty(lcColumn) Then
         lcColumn = Me.CurrentCell.OwningColumn.Name 
      End If
      For Each loRow As DataGridViewRow In Me.Rows
         If loRow.Cells(lcColumn).EditedFormattedValue.ToString.ToUpper().StartsWith(tcValue.ToUpper()) Then
            Me._BS.Position = loRow.Index
            Return True
         End If
      Next
   End If
   Return False
End Function


If your problem is still not solved :

The right Syntax for that use is :

dgvsearchitem.rows(yourIndex).Selected = True


这篇关于从datagridview中的选定行滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆