我正在学习VB.NET。
尝试设置CurrentCell的值时,我遇到了DataGridView组件的问题。 我想做的是:
我有一个带有值的DataGridView。 我想在我的表单中创建一个按钮,当点击它时,我想将选择从当前行更改为下一行。要解释更多,通过单击我的按钮我想模拟鼠标点击DataGridview的效果。
我希望你能帮助我,
谢谢!
答案 0 :(得分:21)
也许是这样的:
If DataGridView1.RowCount > 0 Then
Dim MyDesiredIndex As Integer = 0
If DataGridView1.CurrentRow.Index < DataGridView1.RowCount - 1 Then
MyDesiredIndex = DataGridView1.CurrentRow.Index + 1
End If
DataGridView1.ClearSelection()
DataGridView1.CurrentCell = DataGridView1.Rows(MyDesiredIndex).Cells(0)
DataGridView1.Rows(MyDesiredIndex).Selected = True
End If
注1:也许这两行不是必需的。我还没证明它
DataGridView1.ClearSelection()
DataGridView1.CurrentCell = DataGridView1.Rows(MyDesiredIndex).Cells(0)
注2:请注意,如果我们在最后一行,则会转到第一行
答案 1 :(得分:3)
您需要将特定行的Selected属性设置为true。我认为VB会是这样的:
someDGV.Rows(index).Selected = True
答案 2 :(得分:2)
如果您的数据网格绑定到BindingSource,最好更改其位置:
Object key = Convert.ToInt32(cdr["WordList"]);
int itemFound = lexiconNamesBindingSource.Find("ID_Name", key);
lexiconNamesBindingSource.Position = itemFound;
...你可能需要完成它:
lexiconNamesBindingSource.ResetBidings();
(这是一个旧线程,但我找到了,所以其他人可能会觉得这很有用)
答案 3 :(得分:2)
只需使用BindingSource.MoveNext()
和BindingSource.MovePrevious()
方法。
答案 4 :(得分:0)
你可以这样做:
If DataGridView1.CurrentRow.Index < DataGridView1.Rows.Count Then
DataGridView1.Rows(DataGridView1.CurrentRow.Index + 1).Selected = True
End If
答案 5 :(得分:0)
要获取所选行,您应该使用 SelectedRows(0).Index ,尽管有CurrentRow。因为如果你在programmaticaly中选择了一行,那么下次你会在 CurrentRow.Index 中找到0。所以它会像:
If DataGridView1.SelectedRows(0).Index < DataGridView1.RowCount - 1 Then
MyDesiredIndex = DataGridView1.SelectedRows(0).Index + 1
End If
DataGridView1.Rows(MyDesiredIndex).Selected = True
答案 6 :(得分:0)
扩展上面的答案,这是完美的,因为我花了至少4个小时来为此做好准备。 并假设您的datagridview被称为dgvDevices ...此代码将处理您在行上前后移动时跳出的事件
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
Try
dgvDevices.ClearSelection()
Dim currentr As Integer = dgvDevices.CurrentCell.RowIndex
dgvDevices.CurrentCell = dgvDevices.Rows(currentr - 1).Cells(0)
dgvDevices.Rows(currentr - 1).Selected = True
Catch ex As Exception
dgvDevices.CurrentCell = dgvDevices.Rows(0).Cells(0)
dgvDevices.Rows(0).Selected = True
End Try
End Sub
Private Sub btnForw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForw.Click
Try
dgvDevices.ClearSelection()
Dim currentr As Integer = dgvDevices.CurrentCell.RowIndex
dgvDevices.CurrentCell = dgvDevices.Rows(currentr + 1).Cells(0)
dgvDevices.Rows(currentr + 1).Selected = True
Catch ex As Exception
dgvDevices.CurrentCell = dgvDevices.Rows(dgvDevices.RowCount - 1).Cells(0)
dgvDevices.Rows(dgvDevices.RowCount - 1).Selected = True
End Try
End Sub
答案 7 :(得分:0)
除了Javiers正确答案之外,如果您使用BindingSource作为数据网格视图,那么最好从绑定源更改所选项目而不是使用datagridview.CurrentCell
:
' Example Definitions
Dim bsExample As New BindingSource
Dim dgv As New DataGridView
dgv.DataSource = bsExample
' Example code to change current row position
Dim desiredIndex As Integer = 10
bsExample.Position = desiredIndex
答案 8 :(得分:0)
Private Sub DGW2_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DGW2.DataBindingComplete
Dim mygrid As DataGridView
mygrid = CType(sender, DataGridView)
mygrid.ClearSelection()
End Sub
答案 9 :(得分:0)
input
答案 10 :(得分:0)
易于扩展
Imports System.Runtime.CompilerServices
Module Extensions
''' <summary>
''' Select the Row(Index)
''' </summary>
''' <param name="Dgv">The DataGridView</param>
''' <param name="Index">The Row Index (0 First)</param>
''' <param name="ScroolToTop">If True scrool the row to top</param>
''' <returns>True if no error or False if something went wrong</returns>
' USE: if not DataGridView1.GoToRow(30) then MsgBox("Something went wrong!)
<Extension()>
Public Function GoToRow(Dgv As DataGridView, Index As Integer, Optional ScroolToTop As Boolean = True) As Boolean
If Index > -1 And Index < Dgv.Rows.Count - 1 Then
With Dgv
.ClearSelection()
.Rows(Index).Selected = True
If ScroolToTop Then .FirstDisplayedScrollingRowIndex = Index
End With
Return True
End If
Return False
End Function
End Module