Updating Values in a Data Grid View With Key Press Data Entry in Numeric Up and Down - vb.net

I am setting up a Data Grid View that contains a list of items, with one column as the Item Number (1, 2, 3) and the second number as the name of the items. I am controlling the input of the Item Number column using a Numeric Up and Down, so when I increase it, the rows are added and the Item Number column is automatically updated (so the user doesn't have to enter this in). However, this only works if I use the numeric up and down by clicking the arrows. If I type in the number (say 4 items), I get an exception (System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index') and the procedure doesn't work. The code is below:
Private Sub numItemNumber_ValueChanged(sender As Object, e As EventArgs) Handles numItemNumber.ValueChanged
While (dgvItems.Rows.Count < numItemNumber.Value)
dgvItems.Rows.Add()
i = numItemNumber.Value
dgvItems.Rows(i).Cells(0).Value = i + 1 'This is where the exception is
End While
End Sub
I added in a KeyPress event to see if that could handle the item entered in but it doesn't.
Private Sub numItemNumber_KeyPress(sender As Object, e As EventArgs) Handles numItemNumber.KeyPress
For i = 0 To numItemNumber.Value - 1
dgvItems.Rows(i).Cells(0).Value = i + 1
Next
End Sub
How can I edit this to include both events (the user using the up and down keys or just entering the number in directly)? Thank you.

Don't you still have to Add the new row before you can enter a value in it?
Private Sub numItemNumber_KeyPress(sender As Object, e As EventArgs) Handles numItemNumber.KeyPress
For i = 0 To numItemNumber.Value - 1
dgvItems.Rows.Add()
dgvItems.Rows(i).Cells(0).Value = i + 1
Next
End Sub

Figured out this works, essentially adding a loop through the rows of the data grid view in the Value Changed event so that it can handle any number > 0. Adding the code here for anyone who possibly needs it:
Private Sub numItemNumber_ValueChanged(sender As Object, e As EventArgs) Handles numItemNumber.ValueChanged
While (dgvItems.Rows.Count < numItemNumber.Value)
For i=0 to numItemNumber.Value-1
dgvItems.Rows.Add()
dgvItems.Rows(i).Cells(0).Value = i + 1
Next
End While
End Sub
The KeyPress Event is not needed.

Related

Auto formatting textbox

I am trying to add multiple texts from a datagridview to a textbox.My multiple texts i mean, the textbox will contain the value of cell(12) of all the rows of the dgvw.Here's my code :
Private Sub Button6_Click_1(sender As Object, e As EventArgs) Handles Button6.Click
For Each row In Selected.dg2.Rows
SendMail.totxt.Text &= row.Cells(12).Value
SendMail.Show()
Next
Now, cell 12 contains E-MAILS.My application is also an EMAIL app. What I want is, in the textbox, a comma(,)will be added automatically after every email address/every row's cell value. Any solution?
Private Sub Button6_Click_1(sender As Object, e As EventArgs) Handles Button6.Click
For Each row In Selected.dg2.Rows
SendMail.totxt.Text &= row.Cells(12).Value
SendMail.totxt.Text &= ", "
Next
totxt.Text = totxt.Text.Remove(totxt.TextLength - 2, 2)
You might also want to have a look at giving your email column a name, so that you can refer to it with row.cells("email").value instead of refering to it by index. This way, if you the number of columns in your datagrid ever change, your program wont break.

How to remove a row from datagridview and reset a particular column (vb.net 2005)

I am using VS 2005 edition. I am developing a winform desktop application. Let's say I have a unbound datagridview, with table like below:
Original datagrid before delete:
If I removed Stuff D (Item No 5), the "Item No" column supposed to reset itself accordingly. The expected output should be:
After delete row:
The "Item No" column is not an autonumber, it's just the number I assigned incrementally as the user add in a new row(new Stuff). I tried using the following code in rowremoved event but failed to achieve the expected output. Please help. Thanks.
If the goal is just showing the record index in cell, then you don't need to assign a value to cell and it's enough to set e.Value = e.RowIndex + 1 in CellFormatting event.
But based on your comment it seems you need those cells have values as well. So you need to handle RowsRemoved and RowsAdded event of DataGridView and refresh the cell value with row number:
Public Sub RefreshRowNumbers(g As DataGridView)
For Each r As DataGridViewRow In g.Rows
r.Cells(1).Value = r.Index + 1
Next
End Sub
Private Sub DataGridView1_RowsRemoved(sender As Object, _
e As DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
RefreshRowNumbers(DirectCast(sender, DataGridView))
End Sub
Private Sub DataGridView1_RowsAdded(sender As Object, _
e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
RefreshRowNumbers(DirectCast(sender, DataGridView))
End Sub

One row selector in all Datagridview

Based on what you see below is the sample of a datagridview that an item has been selected.
Here is my question what if I have a more than 1 Datagridview? I mean 5 Datagridview like this.
All of them contains 1 column only. Based on the 1st image, the row selector or the blue one select an item.
My question is how can I make all of the datagridview only have one row selector?
What happens is when i selected each of them all of it has a row selected 5 selections.
How can I make 1 row selector for all of them.
Thinking of changing the Selection Color but I think that's not applicable.
TYSM for future help.
If you're looking for an alternative, you can also try this approach:
Private Sub DataGridView_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles _
DataGridView1.CellEnter, DataGridView2.CellEnter, DataGridView3.CellEnter, DataGridView4.CellEnter, DataGridView5.CellEnter
Dim MyDataGrids() As DataGridView = {DataGridView1, DataGridView2, DataGridView3, DataGridView4, DataGridView5}
For i = 0 To MyDataGrids.Count - 1
If MyDataGrids(i).Name = sender.Name Then
Continue For
Else
MyDataGrids(i).ClearSelection()
End If
Next
End Sub
MyDataGrids() is an array of DataGridViews. If for example, the controls you need to check increases, just add the name of the DataGridView in this array and it will be included in the checking and clearing of selections. Don't also forget the Handles event. As you can see here, all of the five grids .CellEnter event are included so you don't have to copy-paste it to five separate events.
Try this maybe it is more easy to edit if you add more grid
Private Sub ClearSelectedCells(ByVal Identifier As Integer)
If Identifier = 1 Then 'for datagrid 1
dg2.ClearSelection()
dg3.ClearSelection()
ElseIf Identifier = 2 Then 'for datagrid 2
dg1.ClearSelection()
dg3.ClearSelection()
'and so on
.
.
End If
End Sub
Private Sub dg1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dg1.CellClick
ClearSelectedCells(1)
End Sub
'and other gridcellclick
.
.

vb.net code reads lables wrong

Okay, so I am making a game, like cookie clicker, or in my case - http://www.silvergames.com/poop-clicker (don't ask...), so that when you click on the icon in the center, it changes the total value by adding 1. On the side, you have a picture which you click to increase the amount it generates automatically every second.
At the moment I have it like this:
The timer tics every second. If the total amount > the cost of upgrade then it shows the picture of the thing you click to upgrade.
When you click that picture -
The cost is taken away from the total amount.
It changes the amount of times you have used that upgrade by +1.
The automatic upgrades per second is changed by +1.
The Cost is increased by 10.
What is happening is that I click the icon in the middle say 5 times (very quickly) and it only comes up with a total of 3. That in itself is a problem, but the even worse problem is that it shows the picture to click, when i told it to only show when the total value was > 10 (the cost of the upgrade).
I am really confused, and any help will be much appreciated.
Thanks
SkySpear
PS. Here's the Code -
Public Class Form1
Private Sub picPoop_Click(sender As Object, e As EventArgs) Handles picPoop.Click
lblPoops.Text = lblPoops.Text + 1
End Sub
Private Sub picCursor_Click(sender As Object, e As EventArgs) Handles picCursor.Click
lblPoops.Text = lblPoops.Text - lblCursorCost.Text
lblCursorAmmount.Text = lblCursorAmmount.Text + 1
lblPoopsPerSec.Text = lblPoopsPerSec.Text + 1
lblCursorCost.Text = lblCursorCost.Text + 10
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblCursorAmmount.Text = 0
lblCursorCost.Text = 10
lblBabyAmmount.Text = 0
lblBabyCost.Text = 100
lblBowlAmmount.Text = 0
picCursor.Hide()
tmrSec.Start()
End Sub
Private Sub tmrSec_Tick(sender As Object, e As EventArgs) Handles tmrSec.Tick
If lblPoops.Text > lblCursorCost.Text Then picCursor.Show()
End Sub
End Class
Again, don't ask where this ridiculous idea came from, I can assure you it wasn't mine.
Your main problem with this is that in your Timer sub, you are comparing text to text. In this case, a value of "3" > "21", since text comparisons work on a char by char basis. When this happens, your pictureBox is being shown. As others suggested, you can use any of the string to numeric conversion functions in your timer event to make this work better.
A slightly better approach would be to declare some class level numeric variables that hold each individual value and displays them when needed. As an example
numPoops += 1
lblPoops.Text = numPoops
This will make sure that all math will work correctly.
You are dealing with the Value of the textboxes, not the Text in it.
You should enclose each textbox with VAL() to get its exact value as a number.
Private Sub picPoop_Click(sender As Object, e As EventArgs) Handles picPoop.Click
lblPoops.Text = VAL(lblPoops.Text) + 1
End Sub
Private Sub picCursor_Click(sender As Object, e As EventArgs) Handles picCursor.Click
lblPoops.Text = VAL(lblPoops.Text) - VAL(lblCursorCost.Text)
lblCursorAmmount.Text = VAL(lblCursorAmmount.Text) + 1
lblPoopsPerSec.Text = VAL(lblPoopsPerSec.Text) + 1
lblCursorCost.Text = VAL(lblCursorCost.Text) + 10
End Sub
Private Sub tmrSec_Tick(sender As Object, e As EventArgs) Handles tmrSec.Tick
If VAL(lblPoops.Text) > VAL(lblCursorCost.Text) Then picCursor.Show()
End Sub

Getting row number in a DataGridView

How do you get row number DataGridView cell? Specifically, if a user has selected a single cell, how can you get that row number? It needs to access a particular cell based on what the user has selected.
I know that the RemoveAt method can be used to remove at the Focus, but you cannot get the row number at focus apparently?
Thanks for the help!
You can simply use RowIndex on the current cell:
var row = dataGridView1.CurrentCell.RowIndex;
this one works fine .
Private Sub DataGridView1_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
If e.RowIndex >= 0 Then
Me.DataGridView1.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
End If
End Sub
It is nearly the same but you may also use this solution:
var row = dataGridView1.CurrentRow.Index
Another way if you need to track user interaction with a DataGridView:
In my case there is extra processing in some generic functions that use the Me.I_SelCol and Me.I_SelRow column and row coordinates, but I haven't shown that because it's not relevant to the OP.
Best regards,
Rob
Private Sub I_DataGridView_CurrentCellChanged(sender As Object, e As EventArgs) Handles I_DataGridView.CurrentCellChanged
If Me.I_DataGridView.CurrentCellAddress.X < 0 Or Me.I_DataGridView.CurrentCellAddress.Y < 0 Then Exit Sub
' The Windows Me.I_DataGridView object will have already deselected the current cell and selected the
' new cell as per user navigation using mouse or cursor keys. We just need to store the current
' co-ordinates for the currently selected cell.
Me.I_SelCol = Me.I_DataGridView.CurrentCellAddress.X
Me.I_SelRow = Me.I_DataGridView.CurrentCellAddress.Y
Exit Sub
If you in event procedure of datagridview you will found "e As DataGridViewCellEventArgs" you can get row number with e.RowIndex and column number with e.ColumnIndex
Private Sub myDGV_CellLeave(sender As Object, e As DataGridViewCellEventArgs) Handles myDGV.CellLeave
Dim myRow As Integer = e.RowIndex
End Sub
And if you in normal procedure you can get row number like this:
Private Sub btnGetRow_Click(sender As Object, e As EventArgs) Handles btnGetRow.Click
dim myRow1 as Integer = 0
dim myRow2 as Integer = 0
myRow1 = myDGV.CurrentCell.RowIndex
myRow2 = myDGV.CurrentRow.Index
End Sub