Programmatically change selection on DatagridView (.NET) - vb.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!

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

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

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)

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

You could do it this way:
If DataGridView1.CurrentRow.Index < DataGridView1.Rows.Count Then
DataGridView1.Rows(DataGridView1.CurrentRow.Index + 1).Selected = True
End If

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

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

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

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

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

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

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

Related

VB 2013 load datagridview from a form

I have searched the net looking for how to populate a DataGridView with the New Data "Record" that is entered into a form then save the record using the BindingNavigator.
I have a single form with the DataDridView on it and I pulled the table columns from the DataSource into the form so as to allow the people to enter new records. I then want to populate the DataGrid with the information then save it.
Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
Dim RR As Integer = ACCOUNT_MOVEDDataGridView.RowCount - 2
For RC As Integer = 0 To RR
If ACCOUNT_MOVEDDataGridView(2, RC).Value Is DBNull.Value Then
'' ACCOUNT_MOVEDDataGridView(2, RR).Value = ACCOUNT_MOVEDDataGridView(15, RR).Value = 0
End If
Next
End Sub
I have figured it out, I was placing the code under BindingNavigatorAddNewItem_Click
when it should have been under the Save Item click.
Private Sub ACCOUNT_MOVEDBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles ACCOUNT_MOVEDBindingNavigatorSaveItem.Click
Call ACCDEF()
Private Sub ACCDEF()
Dim RR As Integer = ACCOUNT_MOVEDDataGridView.RowCount - 2
For RC As Integer = 0 To RR
If ACCOUNT_MOVEDDataGridView(2, RC).Value Is DBNull.Value Then
ACCOUNT_MOVEDDataGridView(2, RR).Value = Date.Now
ACCOUNT_MOVEDDataGridView(15, RR).Value = 0
Else
ACCOUNT_MOVEDDataGridView(1, RR).Value = Date.Now
End If
Next
End Sub
Which loads the DataGridView with the default values, Thanks to all that have looked at my problem.

Keep focus on row after datagridview update

I'm creating an VB windows application. The point of the application is a simple DataGridView where I'm fetching a View from a SQL Server database.
The DataGridView is refreshed every second so I could see new data income in my GridView.
The problem is keeping focus on row after the refresh. I need the solution, where after I click a row, or a cell it keeps me on it even after the refresh.
Here is my code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Refresh every 1 sec
Dim timer As New Timer()
timer.Interval = 1000
AddHandler timer.Tick, AddressOf timer_Tick
timer.Start()
'TODO: This line of code loads data into the 'XYZDataSet.view1' table. You can move, or remove it, as needed.
Me.View1TableAdapter.Fill(Me.XYZDataSet.view1)
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
If Me.DataGridView1.Rows(i).Cells("DayTillDelivery").Value <= 30 Then
Me.DataGridView1.Rows(i).Cells("DayTillDelivery").Style.ForeColor = Color.Red
End If
Next
End Sub
Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
'Calling refresh after 1 second and updating the data
Me.DataGridView1.Refresh()
Me.View1TableAdapter.Fill(Me.XYZDataSet.view1)
End Sub
End Class
I've solved a similar problem in the past by storing the indexes of the selected cell in a variable before doing the refresh, so I'm able to restore the selection by calling DataGridView.Rows(selRow).Cells(selCol).Selected = True after the update.
Edit - Sample Code:
To later readers:Please take a look at Edit#2 where I describe a better method to re-select the previous selected cell!
Sample Code:
' Variables for remembering the indexes of the selected cell
Dim selRow As Integer
Dim selCol As Integer
' Check if there is a selected cell to prevent NullPointerException
If DataGridView1.SelectedCells().Count > 0 Then
selRow = DataGridView1.CurrentCell.RowIndex
selCol = DataGridView1.CurrentCell.ColumnIndex
End If
' Dummy "update"
' don't forget to clear any existing rows before adding the new bunch (only if you always reloading all rows)!
DataGridView1.Rows.Clear()
For index = 1 To 20
DataGridView1.Rows.Add()
Next
' Check if there are "enough" rows after the update,
' to prevent setting the selection to an rowindex greater than the Rows.Count - 1 which would
' cause an IndexOutOfBoundsException
If (DataGridView1.Rows.Count - 1) > selRow Then
' Clear selection and then reselect the cell that was selected before by index
DataGridView1.ClearSelection()
' For the next line of code, there is a better solution in Edit #2!
DataGridView1.Rows(selRow).Cells(selCol).Selected = True
End If
Please note:
This procedure requires you to add the rows in the exact same order that you have added them before the update, as only the .Index of the selected row is stored in the variable. If you readding the rows in a different order, then not the same row but the row at the same position will be selected after the refresh.
You should add check if there is a selected row at all (to prevent a NullPointerException) and if there are "enough" rows in the DataGridView after the refresh, to prevent an IndexOutOfBoundsException.
This only works if the DataGridView1.SelectionMode is to something that actually selects rows, like FullRowSelect.
Don't forget to clear any existing rows before adding new ones by updating (only if you always reloading all rows).
Edit 2 - RowHeader triangle and accidental MultiSelect
As stated in the comments below, there was an odd behavior that would lead to an accidental MultiSelect, if the user holds down the mouse button past the refresh cycle. Also, the RowHeader triangle was not set to the correct row.
After some research I found a solution to this behavior. Instead of setting the .Selected-property of a given cell to True, set the .CurrentCell-property of the DataGridView to the cell you would like to select!
In code, this means changing
DataGridView1.Rows(selRow).Cells(selCol).Selected = True
to
DataGridView1.CurrentCell = DataGridView1.Rows(selRow).Cells(selCol)
and there you go. :-)
Before Fill, store the CurrentRow values and currenCell column:
Dim currentColumnIndex As Integer = 0 ;
Dim currentValues As List(Of Object) = If(DataGridView1.CurrentRow Is Nothing, Nothing, New List(Of Object)())
If currentValues IsNot Nothing Then
For i As Integer = 0 To DataGridView1.Columns.Count - 1
currentValues.Add(DataGridView1.CurrentRow.Cells(i).Value)
Next
currentColumnIndex = DataGridView1.CurrentCell.ColumnIndex;
End If
After Fill, search the row corresponding to stored values:
Dim i As Integer = 0
While i < DataGridView1.Rows.Count AndAlso currentValues IsNot Nothing
Dim areIdentical As Boolean = True
Dim j As Integer = 0
While j < DataGridView1.Columns.Count AndAlso areIdentical
areIdentical = DataGridView1.Rows(i).Cells(j).Value = currentValues(j)
j += 1
End While
If areIdentical Then
DataGridView1.CurrentCell = DataGridView1.Rows(i).Cells(currentColumnIndex)
currentValues = Nothing
End If
i += 1
End While
Note: the "For/While" loop coding is perhaps not optimal because it results from automatic conversion from C# to vb.net.
C# fix code , next reload pattern
if (dataGridView7.SelectedCells.Count > 0)
{
//MessageBox.Show(selcell + "------"+dataGridView7.CurrentCell.ColumnIndex.ToString());
if (selcell > 0 && dataGridView7.CurrentCell.ColumnIndex==0) { }else
{
selrow = dataGridView7.CurrentCell.RowIndex;
selcell = dataGridView7.CurrentCell.ColumnIndex;
}
}
loaddataJobsall();
dataGridView7.ClearSelection();
dataGridView7.Rows[selrow].Cells[selcell].Selected = true;

Combo Box Returning -1 For SelectedIndex

I'm trying to pick up a combo box's selected index. This was working absolutely fine, then all of a sudden it started returning -1 no matter what item is selected
My code is:
Form Code
Private Sub Man_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles Man.SelectedIndexChanged, Units.SelectedIndexChanged
'Set Transducer Type
Call References.LevListAdd()
End Sub
References Module LevListAdd Sub
Public Sub LevListAdd()
Form1.Lev.Items.Clear()
If Form1.Man.Text = "Pulsar" Then
With Form1.Lev.Items
.Add("Ultra Range")
.Add("IMP Range")
.Add("Twin Range")
End With
End If
End Sub
This fills the combo box lev fine when the Man combo box item "Pulsar" is selected. I then want my users to click a button to generate some labels and stuff. The code is as such:
Button Click Code
Private Sub Generate_Click(sender As Object, e As EventArgs) Handles Generate.Click
If CheckGenerate() = False Then Exit Sub
Dim X = CheckGenerationType(Man.SelectedIndex, Lev.SelectedIndex, Level.Checked, Volume.Checked, ListBox1.SelectedIndex,
Units.SelectedIndex)
Call ParamPage(X)
End Sub
CheckGenerate simply checks that all boxes have been filled in. I pass the informtion from the form to the following function:
Public Function CheckGenerationType(Man As Integer, Lev As Integer, Level As Boolean, Volume As Boolean, TankType As Integer,
MeasurementUnit As Integer) As String
Dim M As Integer
Dim L As Integer
Dim CT As Integer
Dim TT As Integer
Dim Ms As Integer
M = Man
L = Lev
TT = TankType
Ms = MeasurementUnit
If Level = True Then
CT = 0
ElseIf Volume = True Then
CT = 1
End If
CheckGenerationType = CStr(M) + "." + CStr(L) + "." + CStr(CT) + "." + CStr(TT) + "." + CStr(Ms)
End Function
When the lev.selectedindex parameter arrives at this function, it reads -1. Even if the user has selected any of the 3 items. Can anyone explain why this is happening?
I've just tried your code. I get the same result (-1 in lev.SelectedIndex) and this was because jumped using tab through the controls my when i'm hitting the Man or Units Combobox it runs the LevListAdd() and then clears the Lev-ComboBox because of Form1.Lev.Items.Clear().
You should think about your call Man_SelectedIndexChanged_1 or maybe just use something like this:
Public Sub LevListAdd()
If Form1.Man.Text = "Pulsar" Then
Form1.Lev.Items.Clear()
instead of
Public Sub LevListAdd()
Form1.Lev.Items.Clear()
If Form1.Man.Text = "Pulsar" Then
And you should separate the calls from the man and unit ComboBoxes.
Private Sub Unit_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles Units.SelectedIndexChanged
' Do something
End Sub
Private Sub Man_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles Man.SelectedIndexChanged
Form1.Lev.Items.Clear()
Select Case Form1.Man.Text
Case "Pulsar"
With Form1.Lev.Items
.Add("Ultra Range")
.Add("IMP Range")
.Add("Twin Range")
End With
Case "animals"
With Form1.Lev.Items
.Add("dogs")
.Add("cats")
.Add("birds")
End With
End Select
End Sub

Checkbox returned in Event Handler isn't one on form

I'm creating an array of checkboxes on a form dynamically; the code that creates the array looks like this:-
checkbox_array(count_of_checkboxes) = New CheckBox
if (count_of_checkboxes = 0) Then
checkbox_array(count_of_checkboxes).Top = specimen_checkbox.Top
checkbox_array(count_of_checkboxes).Left = specimen_checkbox.Left
else
checkbox_array(count_of_checkboxes).Top = checkbox_array(count_of_checkboxes - 1).Top + vertical_offset
checkbox_array(count_of_checkboxes).Left = checkbox_array(count_of_checkboxes - 1).Left + horizontal_offset
End If
my_panel.Controls.Add(checkbox_array(count_of_checkboxes))
AddHandler checkbox_array(count_of_checkboxes).MouseClick, cbxSpecimen_CheckedChanged
checkbox_array(count_of_checkboxes).Name = someValue
checkbox_array(count_of_checkboxes).Text = someValue
checkbox_array(count_of_checkboxes).Enabled = true
checkbox_array(count_of_checkboxes).Visible = true
checkbox_array(count_of_checkboxes).Show()
This works fine and dandy on one form. However, I am using the same code on a form which is derived from a base form, and running into a problem, in that the object returned in the sender parameter, although clearly a checkbox with a recognisable name, isn't any of the checkboxes in the array.
I verified this with:-
Private Sub cbxSpecimen_CheckedChanged( sender As System.Object, e As System.EventArgs) Handles cbxSpecimen.CheckedChanged
For i As Integer = 0 To checkbox_array.GetUpperBound(0) - 1
If checkbox_array(i).Equals(sender) Then
// set a breakpoint here
End If
Next i
End Sub
Can anyone shed any light on why this should work on a normal form, but not a derived-class form?
I verified this with:-
Private Sub cbxSpecimen_CheckedChanged( sender As System.Object, e As System.EventArgs)
Handles cbxSpecimen.CheckedChanged
For i As Integer = 0 To checkbox_array.GetUpperBound(0) - 1
If checkbox_array(i).Equals(sender) Then
// set a breakpoint here
End If
Next i
End Sub
Why checkbox_array.GetUpperBound(0) - 1? This will skip the last element in the array. Try:
For i As Integer = 0 To checkbox_array.GetUpperBound(0)
If checkbox_array(i).Equals(sender) Then
// set a breakpoint here
End If
Next i
Or
For i As Integer = 0 To checkbox_array.Length - 1
...
I have managed to get this to work by refilling the array of checkboxes inside the click event:-
Private Sub cbxSpecimen_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles cbxSpecimen.CheckedChanged
For i As Integer = 0 To check_boxes.GetUpperBound(0)
If check_box_array(i).Name = CType(sender, CheckBox).Name And
Not check_box_array(i).Equals(sender) Then
check_box_array(i) = CType(sender, CheckBox)
End If
Next i
' do useful work
End Sub
After the check box on the form has been stuffed back into the array, it remains there (so the second invokation for the same checkbox doesn't insert into the array a second time).
This seems like a ghastly hack to me, but I'll go with it for the time being.

Incrementing Label Value when a button is clicked

I have tried converting using CInt, TryParse, Convert.Int32, etc but nothing worked. I just need to display the total votes of the candidated when a button btnVote is clicked and display the result when I click btnResult. I've tried searching on the net also but still no luck so I came up to this website and hopefully I could solve my problem with my program in VB.net
Below is my code:
Public Class Presidential_Election
Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click
If cboCandidates.Text = "NOLI" Then
lblNoli.Text = Integer.Parse(lblNoli.Text) + 1
ElseIf cboCandidates.Text = "VAL" Then
lblVal.Text = (CInt(lblVal.Text) + 1).ToString
ElseIf cboCandidates.Text = "LESTER" Then
lblLester.Text = CInt(lblLester.Text) + 1
ElseIf cboCandidates.Text = "GWAPO" Then
lblGwapo.Text = CInt(lblGwapo.Text) + 1
End If
End Sub
Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click
lblNoli.Visible = True
lblVal.Visible = True
lblLester.Visible = True
lblGwapo.Visible = True
End Sub
End Class
I would set an integer counter for each candidate. Set these to 0 and then increment them as they receive a vote. When you display the results just bind the text of the label to its respective counter and make it visible. This way, you don't have to keep referencing label.text.
If you haven't set the label text to be 0 at the beginning, using CInt would definitely throw an error. You have two options :
set all labels text to 0 and keep using CInt, or
use Val()
Example :
lblGwapo.Text = Val(lblGwapo.Text) + 1