Automatically increment selected row through DataGridView - vb.net

I am trying to update a selected row to a number and increment every other row after that by 1 from the newly updated value, leaving all previous rows unaffected
I have the selected row to update figured out:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim i = DataGridView1.CurrentRow.Index
With DataGridView1
.Rows(i).Cells("Value").Value = NumTextbox.Text
End With
I tried adding:
.Rows(i).Cells("Value").Value = i + 1
But only labels the value to the number of the row, can anyone point me in the right direction?

Putting it all together...
Private Sub IncrementRows()
Dim i = DataGridView1.CurrentRow.Index
Dim num As Integer
If Integer.TryParse(NumTextbox.Text, num) Then
With DataGridView1
.Rows(i).Cells("Value").Value = num
End With
For j As Integer = i + 1 To DataGridView1.Rows.Count - 1
num += 1
DataGridView1.Rows(j).Cells("Value").Value = num
Next
Else
MessageBox.Show("Please enter a valid number.")
NumTextbox.Focus
End If
End Sub

For j As Integer = i + 1 to DataGridView1.Rows.Count - 1
DataGridView1.Rows(j).Cells("Value").Value = i + 1
next

Related

Make a Auto slide show

I am trying to create slide show I loaded the picture in picture box 2 through form load and in the timer options left him enabled here is my code and for some reason it is not changing pictures I also tried to do it with out loading a picture through form load same thing not changing just blank
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim max As Integer = 10000
Dim rnd As New Random
Dim rand As Integer = rnd.Next(10, max + 1)
Dim i As Integer = 1
Dim number(max - 1) As Integer
For i = 0 To max - 1
If number(i) = rand Then
rand = rnd.Next(1, max + 1)
i = -1
ElseIf number(i) = 0 Then
number(i) = rand
rand = rnd.Next(1, max + 1)
If i = max - 1 Then
Exit For
End If
i = -1
End If
Next
Timer1.Interval = number(i)
i += 1
ChangeImage()
End Sub
Private Sub ChangeImage()
Static Dim iImage1 As Integer
Select Case iImage1
Case 0
PictureBox2.Image = My.Resources.Image2
iImage1 += 1
Case 1
PictureBox2.Image = My.Resources.Classic_Burger_SpendWithPennies__2
iImage1 += 1
Case 2
PictureBox2.Image = My.Resources.Image4
iImage1 += 1
Case 3
PictureBox2.Image = My.Resources.Image5
iImage1 += 1
Case 4
PictureBox2.Image = My.Resources.Classic_Burger_SpendWithPennies__2
iImage1 += 1
End Select
I want just to change a picture every 10 seconds in picture box2
Make sure the Interval property of your Timer is set to 10,000.
Then use code like this:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Static rnd As New Random
Dim i As Integer = rnd.Next(5) ' 0 to 4 inclusive
Select Case i
Case 0
PictureBox2.Image = My.Resources.Image2
Case 1
PictureBox2.Image = My.Resources.Classic_Burger_SpendWithPennies__2
Case 2
PictureBox2.Image = My.Resources.Image4
Case 3
PictureBox2.Image = My.Resources.Image5
Case 4
PictureBox2.Image = My.Resources.Classic_Burger_SpendWithPennies__2
End Select
End Sub
But I suspect there is more to this story. What were you trying to do with the Interval?
It's because you dont pass the parameter.
In your sub ChangeImage, you set a new variable.
Change like this :
ChangeImage(i)
End Sub
Private Sub ChangeImage(iImage1)
Select Case iImage1

How To Sum DataGridView Amount Column auto VB.NET

How i Sum Datagridview Column auto sum when i adding the digit i have use the below code but not working
Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
Dim sum = (From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
Select CDec(row.Cells(1).Value)).Sum
TextBox1.Text = sum.ToString
End Sub
Dim sum As Integer = 0
For i As Integer = 0 To DataGridView1.Rows.Count() - 1 Step +1
sum = sum + DataGridView1.Rows(i).Cells(2).Value
Next
TextBox5.Text = sum.ToString()
try like this
Dim total As Integer = DataGridView1.Rows.Cast(Of DataGridViewRow)().Sum(Function(t) Convert.ToInt32(t.Cells(1).Value))
TextBox1.Text = total.tostring()

How to count selected items in the listbox by just select and see result in a label?

getting the logic of counting is easy but in practice it gets hard sometimes.
Now I've a list with many items on it. how to count those items if there were repeated and i want to convert that to a number using the FOR loop since i know how many items in the list.
I tried some codes but i did not succeed
'''''''''''''''
' VB 2015
''''''''''''
Public Class Form1
Private Sub lstWinners_List_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWinners_List.SelectedIndexChanged
If lstWinners_List.SelectedIndex <> -1 Then
Dim count As Integer = 0
Dim strselection As String = lstWinners_List.Items(lstWinners_List.SelectedIndex).ToString
For i As Integer = 0 To lstWinners_List.Items.Count - 1
If lstWinners_List.Items(i) = strselection Then
count = count + 1
End If
Next
lblOutput.Text = count.ToString
End If
End Sub
End Class
for EX:
i wanna count the word "michigan " how many times repeated in the list by just clicking on it ?
Here's an example using Jim Hewitt's comment:
Private Sub lstWinners_List_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWinners_List.SelectedIndexChanged
If lstWinners_List.SelectedIndex <> -1 Then
Dim selection As String = lstWinners_List.Items(lstWinners_List.SelectedIndex).ToString
Dim wins As Integer = (From team As String In lstWinners_List.Items Where team.Equals(selection)).Count
lblOutput.Text = wins.ToString
End If
End Sub
Edit
Here's an equivalent, manual, indexed for loop:
Private Sub lstWinners_List_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWinners_List.SelectedIndexChanged
If lstWinners_List.SelectedIndex <> -1 Then
Dim count As Integer = 0
Dim selection As String = lstWinners_List.Items(lstWinners_List.SelectedIndex).ToString
For i As Integer = 0 To lstWinners_List.Items.Count - 1
If lstWinners_List.Items(i) = selection Then
count = count + 1
End If
Next
lblOutput.Text = count.ToString
End If
End Sub
Dim count As Integer = 0
For Each n As String In ListBox1.Items
If n = "red" Then
count += 1
End If
Next
lblOutput.Text(count)
something like this do you mean?

Iterating DataGridView gets only the values of the first two columns

I have a DataGridView that has got 5 columns. I am using a For Next loop to iterate each column's value, but for some reason it only works for column 1 and 2.
Here is my code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As Decimal
Dim b As Decimal
Dim i As Integer
a = 5.23
For i = 0 To DataGridView1.Rows.Count - 1
b = a * (DataGridView1.Item(i, 0).Value)
MessageBox.Show(b)
Next
End Sub
If you want to iterate each column of the first row then you need to loop up to DataGridView.Columns.Count - 1 instead.
For i = 0 To DataGridView1.Columns.Count - 1

Delete Datagridview Cell(s) Selection

What I want to do is select rows(0).cells(0) to rows(1).cells(6) in a datagridview (just an example, selection will change), press a button and then all cells in the selected range delete or equal "".
Any help will be appreciated. Thanks
Here is the code:
Private Sub clearpb_Click(sender As Object, e As EventArgs) Handles clearpb.Click
Dim myrowcount As Integer = Me.datagridviewentry.Rows.Count
Dim mycolcount As Integer = Me.datagridviewentry.Columns.Count
a = 0
Do While a < myrowcount
b = 0
Do While b < mycolcount
If Me.datagridviewentry.Rows(a).Cells(b).Selected = True Then
Me.datagridviewentry.Rows(a).Cells(b).Value = ""
End If
b += 1
Loop
a += 1
Loop
End Sub