Dali
Dali

Reputation: 153

Programmatically change selection on DatagridView (.NET)

I'm learning 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 :

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.

I hope you can help me,

Thanks!

Upvotes: 15

Views: 89108

Answers (12)

Samir RAMDANI
Samir RAMDANI

Reputation: 23

I have DataGridView like that : | ID | Name | |:---|:-----:| | 1 | Samir |

I search by 'Name' using this code :

    Private Sub GunaButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GunaButton6.Click
            Dim temp As Integer = 0
            For i As Integer = 0 To Me.Listofclient.RowCount - 2
                If Me.Listofclient.Rows(i).Cells(1).Value.ToString = EDSearch.Text Then
                    Me.Listofclient.ClearSelection()
                    Me.Listofclient.Rows(i).Selected = True
                    temp = 1
                End If
            Next
            If temp = 0 Then
                MsgBox("Item not found")
            End If
        End Sub

Upvotes: 0

IfThenElse
IfThenElse

Reputation: 509

Easy with Extensions

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

Upvotes: 0

 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

Upvotes: 0

DrSerhat
DrSerhat

Reputation: 11

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

Upvotes: 0

Mojtaba Rezaeian
Mojtaba Rezaeian

Reputation: 8736

Besides Javiers correct answer, if you're using BindingSource for your datagridview then it will be better to change selected item from binding source rather than using 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

Upvotes: 0

Gan
Gan

Reputation: 21

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

Upvotes: 2

Francisco Cortes
Francisco Cortes

Reputation: 1226

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

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

Upvotes: 0

sikarnarender
sikarnarender

Reputation: 86

To get the selected row, you should use SelectedRows(0).Index inspite 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 :

If DataGridView1.SelectedRows(0).Index < DataGridView1.RowCount - 1 Then
    MyDesiredIndex = DataGridView1.SelectedRows(0).Index + 1
End If

DataGridView1.Rows(MyDesiredIndex).Selected = True

Upvotes: 0

Alex Russell
Alex Russell

Reputation: 209

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

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)

Upvotes: 2

Javier
Javier

Reputation: 4141

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

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

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

Upvotes: 28

splattne
splattne

Reputation: 104040

You could do it this way:

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

Upvotes: 0

Beep beep
Beep beep

Reputation: 19151

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

someDGV.Rows(index).Selected = True

Upvotes: 5

Related Questions