vb.net 以编程方式更改 DatagridView (.NET) 上的选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/575015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:04:46  来源:igfitidea点击:

Programmatically change selection on DatagridView (.NET)

vb.netdatagridview

提问by Dali

I'm learning VB.NET.

我正在学习 VB.NET。

I've a problem with DataGridView component when trying to set the value of the CurrentCell. What i'm trying to do is :

尝试设置 CurrentCell 的值时,DataGridView 组件出现问题。我想要做的是:

I've a DataGridView With values. I want to make a button in my forms and when clicking on it I want to change the selection from the current row to the next. To explain more, by clicking my Button I want to simulate the effect of a mouse click on a DataGridview.

我有一个带有值的 DataGridView。我想在我的表单中创建一个按钮,当单击它时,我想将选择从当前行更改为下一行。为了解释更多,通过单击我的按钮,我想模拟在 DataGridview 上单击鼠标的效果。

I hope you can help me,

我希望你可以帮助我,

Thanks!

谢谢!

回答by Javier

Maybe something like this:

也许是这样的:

    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

Note 1:maybe these two lines are not necessary. I haven′t proved it

注1:也许这两行不是必需的。我还没有证明

        DataGridView1.ClearSelection()            
        DataGridView1.CurrentCell = DataGridView1.Rows(MyDesiredIndex).Cells(0)

Note 2:note that if we are in the last row, it goes to first

注意2:注意,如果我们在最后一行,它会转到第一行

回答by Beep beep

You need to set the particular row's Selected property to true. I think the VB would be something like this:

您需要将特定行的 Selected 属性设置为 true。我认为VB会是这样的:

someDGV.Rows(index).Selected = True

回答by Alex Russell

If your data grid is bound to a BindingSource, it is better to change the position there:

如果您的数据网格绑定到 BindingSource,最好更改那里的位置:

Object key = Convert.ToInt32(cdr["WordList"]);
int itemFound = lexiconNamesBindingSource.Find("ID_Name", key);
lexiconNamesBindingSource.Position = itemFound;

...and you might need to finish it off with:

...你可能需要用以下方法完成它:

lexiconNamesBindingSource.ResetBidings();

(This is an old thread, but I found it, so someone else might find this useful)

(这是一个旧线程,但我找到了,所以其他人可能会觉得这很有用)

回答by Gan

Just use the BindingSource.MoveNext()and BindingSource.MovePrevious()methods.

只需使用BindingSource.MoveNext()BindingSource.MovePrevious()方法。

回答by sikarnarender

To get the selected row, you should use SelectedRows(0).Indexinspite of CurrentRow. Because if you programmaticaly make a row as selected, then next time you will find 0 in CurrentRow.Index. So It would be like :

要获取选定的行,您应该使用SelectedRows(0).Index 尽管CurrentRow。因为如果您以编程方式将一行设置为选定的,那么下次您会在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

回答by Francisco Cortes

expanding on the answer above which is perfect considering I spent at least 4 hours loooking for this. and assuming that your datagridview is called dgvDevices... this code will handle the event in which you go outbounce as you move back and forward on your rows

扩展上面的答案,考虑到我花了至少 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

回答by Mojtaba Rezaeian

Besides Javiers correct answer, if you're using BindingSourcefor your datagridview then it will be better to change selected item from binding source rather than using datagridview.CurrentCell:

除了 Javiers 的正确答案,如果您为 datagridview使用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

回答by DrSerhat

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

回答by Edmar Decierdo Ceniza

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If DataGridView1.Rows.Count - 1 > 0 Then
            For i As Integer = 0 To DataGridView1.Rows.Count - 1 Step +1
                If DataGridView1.Rows.Count - 1 > 0 Then
                    DataGridView1.Rows.RemoveAt(0)
                End If
            Next
        Else

        End If
    End Sub

回答by splattne

You could do it this way:

你可以这样做:

If DataGridView1.CurrentRow.Index < DataGridView1.Rows.Count Then
    DataGridView1.Rows(DataGridView1.CurrentRow.Index + 1).Selected = True
End If