Send text from textboxes to datagrid - vb.net

I have for textboxes on my form and I would like the text there to be send to a datagrid. I wrote the following:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim itemName As String = txtItem.Text
Dim qty As Double = CDbl(txtQTY.Text)
Dim price As Double = CDbl(txtPrice.Text)
Dim Total As Double = price * qty
txtTotal.Text = Convert.ToString(Total)
Dim row As Integer = grdNewInvoice.Rows.Count
Dim data As TextBox() = New TextBox() {txtItem, txtQTY, txtPrice, txtTotal}
grdNewInvoice.Rows.Add()
For i As Integer = 0 To data.Length - 1
grdNewInvoice(i, row).Value = data(0)
Next
End Sub
But I get the following on my datagrid row: System.Windows.Forms.TextBox, Text: [textbox string]
I tried the following code as well to make sure there was nothing wrong with my settings on my datagrid:
'grdNewInvoice.Rows(0).Cells(0).Value = itemName
'grdNewInvoice.Rows(0).Cells(1).Value = qty
'grdNewInvoice.Rows(0).Cells(2).Value = price
'grdNewInvoice.Rows(0).Cells(3).Value = Total
That worked fine and the text went in as expected but since I will be writing to multiple lines on the datagrid, I will need to use a loop.
What am I doing wrong here?

One way to do this is to use a list of string array.
Dim row As Integer = grdNewInvoice.Rows.Count
Dim data As New List(Of String())
data.Add({txtItem.Text, txtQTY.Text, txtPrice.Text, txtTotal.Text})
data.Add({"Item2", "qtr2", "price2", "total2"})
data.Add({"Item3", "qty3", "price3", "total3"})
For i As Integer = 0 To data.Count - 1
grdNewInvoice.Rows.Add(data(i))
Next

Related

How to create a Loop to push Values to different TextBox with Similar Labels names and Change their text?

I am new to this VB. I'm building an application where I need to fill some 48 text boxes with 4 sets of similar names like
L1onHHxx
L1onMMxx
L1offHHxx
L1offMMxx
Where xx is a number from 1 to 24 for each label.
What I'm trying to do is calculate some values based on the user input and fill the text boxes with results.
Private Async Sub Line1Calc_Click(sender As Object, e As EventArgs) Handles Line1Calc.Click
Dim Cycle_Time As Integer
Dim Cycle_Count As Integer
Dim srtHH As Integer
Dim srtMM As Integer
Dim stpHH As Integer
Dim stpMM As Integer
Dim inDelay As Integer
Dim i As Integer
Dim lineONHH As New Label
Dim lineONMM As New Label
Dim lineOFFHH As New Label
Dim lineOFFMM As New Label
'Dim controls = GetAllTextBoxes(Me)
'Line1Tab
Cycle_Time = CInt(L1Time.Text)
Cycle_Count = CInt(L1Count.Text)
srtHH = CInt(L1ONHH.Text)
srtMM = CInt(L1ONMM.Text)
stpHH = CInt(L1OFFHH.Text)
stpMM = CInt(L1OFFMM.Text)
inDelay = (24 / CInt(Cycle_Count))
L1onhh1.Text = srtHH
L1onhh1.Text = srtHH
L1offhh1.Text = stpHH
L1offmm1.Text = stpMM
For i = 2 To (Cycle_Count)
srtHH += inDelay
stpHH += inDelay
lineONHH.Name = "L1onhh" & i.ToString()
lineONMM.Name = "L1onmm" & i.ToString()
lineOFFHH.Name = "L1offhh" & i.ToString()
lineOFFMM.Name = "L1offmm" & i.ToString()
lineONHH.Text = srtHH
lineONMM.Text = srtMM
lineOFFHH.Text = stpHH
lineOFFMM.Text = stpMM
' To Check if the labels name are correct
Box1.Text = lineONHH.Name
Box2.Text = lineONMM.Name
Box3.Text = lineOFFHH.Name
Box4.Text = lineOFFMM.Name
Await Task.Delay(1000)
Next
End Sub
Here when I pass the value of the New label to Box 1,2,3 & 4 for test purposes they are correct but the values don't appear on the respective text boxes.
Please can anyone point me in the right direction of what I'm doing wrong?
This should get you close, with a few assumptions made.
It assumes you have constant textbox naming pattern.
Assumes you want hour and minute in string.
Switch that structure to a DateTime if you want it in that format.
'class to hold all your time value entries
Private Class TimeValue
Public Property HourValue As String
Public Property MinValue As String
End Class
'class for each control time slow and its associated timevalues
Private Class ControlTime
Public Property TimeSlot As String
Public Property OnValue As TimeValue
Public Property OffValue As TimeValue
End Class
'method to set control values
Private Function SetControlValus(controlTimeItem As ControlTime) As Boolean
'find the control
Dim OnHourTextBox As TextBox = DirectCast(Me.Controls.Find(String.Concat("L1onHH", controlTimeItem.TimeSlot), False)(0), TextBox)
OnHourTextBox.Text = controlTimeItem.OnValue.HourValue
Dim OnMinTextBox As TextBox = DirectCast(Me.Controls.Find(String.Concat("L1onMM", controlTimeItem.TimeSlot), False)(0), TextBox)
OnMinTextBox.Text = controlTimeItem.OnValue.MinValue
Dim OffHourTextBox As TextBox = DirectCast(Me.Controls.Find(String.Concat("L1offHH", controlTimeItem.TimeSlot), False)(0), TextBox)
OffHourTextBox.Text = controlTimeItem.OffValue.HourValue
Dim OffMinTextBox As TextBox = DirectCast(Me.Controls.Find(String.Concat("L1offMM", controlTimeItem.TimeSlot), False)(0), TextBox)
OffMinTextBox.Text = controlTimeItem.OffValue.MinValue
Return True
End Function
Private Sub Test()
'set up some results
Dim results As List(Of ControlTime) = New List(Of ControlTime)
results.Add(New ControlTime() With {.TimeSlot = "01",
.OnValue = New TimeValue() With {.HourValue = "08", .MinValue = "56"},
.OffValue = New TimeValue() With {.HourValue = "08", .MinValue = "58"}})
results.Add(New ControlTime() With {.TimeSlot = "02",
.OnValue = New TimeValue() With {.HourValue = "09", .MinValue = "14"},
.OffValue = New TimeValue() With {.HourValue = "09", .MinValue = "29"}})
For Each controlTimeItem In results
SetControlValus(controlTimeItem)
Next
End Sub
Private Sub TestControlTimeButton_Click(sender As Object, e As EventArgs) Handles TestControlTimeButton.Click
Try
Test()
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
End Try
End Sub

How I can set the index of Datagridview to 0

How I can set the index of DataGridView to 0?
I developed a pharmacy software where I want to add values from TextBoxes into DataGridView with adding a new row. It is working but the problem is when I clear the value of DataGridView by using this code
dgvSoldMedicineInfo.Rows.Clear()
then when I try the previous procedure it show this error in VB.NET
Index was out of range, Must be non-negative and less than the size of the collection. Parameter name:index
Code:
Dim rowcounter As Integer = 0
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
dgvSoldMedicineInfo.Rows.Add()
dgvSoldMedicineInfo.Rows(rowcounter).Cells("ColumnBarcodeNo").Value = txtBarcodeNo.Text
dgvSoldMedicineInfo.Rows(rowcounter).Cells("ColumnName").Value = dgvSale.CurrentRow.Cells(2).Value
dgvSoldMedicineInfo.Rows(rowcounter).Cells("ColumnQuantity").Value = txtQuantity.Text
dgvSoldMedicineInfo.Rows(rowcounter).Cells("ColumnSalePrice").Value = txtSalePrice.Text
dgvSoldMedicineInfo.Rows(rowcounter).Cells("ColumnAmount").Value = txtTotalAmount.Text
dgvSoldMedicineInfo.Rows(rowcounter).Cells("ColumnReject").Value = "Reject"
dgvSoldMedicineInfo.Rows(rowcounter).Cells("ColumnStockID").Value = dgvSale.CurrentRow.Cells(0).Value
dgvSoldMedicineInfo.Rows(rowcounter).Cells("ColumnStockQuantity").Value = dgvSale.CurrentRow.Cells(3).Value
System.Math.Max(System.Threading.Interlocked.Increment(rowcounter), rowcounter - 1)
End Sub
There's really no point to that rowcounter variable in that context and it's bad form to keep using dgvSoldMedicineInfo.Rows(rowcounter) over and over again. The proper way to do that would be like so:
Dim rowIndex = dgvSoldMedicineInfo.Rows.Add()
Dim row = dgvSoldMedicineInfo.Rows(rowIndex)
Dim cells = row.Cells
cells("ColumnBarcodeNo").Value = txtBarcodeNo.Text
cells("ColumnName").Value = dgvSale.CurrentRow.Cells(2).Value
'etc.
That said, why do that when the Add method is overloaded and will accept all the cell values up front?
dgvSoldMedicineInfo.Rows.Add(txtBarcodeNo.Text,
dgvSale.CurrentRow.Cells(2).Value,
...)

how to check checklistbox items using datagridview vb.net?

I'm just a beginner for coding and I want to programmatically check items in checklistbox using datagridview.
Data grid view values are seperated with commas like this jhon,Metilda,saman,.
Checklistbox name as chklistinput and please help me to solve this ?
'Full coding is here..............................
Private Sub TextBox10_TextChanged(sender As Object, e As EventArgs) Handles TextBox10.TextChanged
'this is ok and searching as I want
Dim SearchV As String = TextBox10.Text
SearchV = "%" + TextBox10.Text + "%"
Me.PassIssuingRecordTableAdapter.FillBy(Me.Database4DataSet.PassIssuingRecord, SearchV)
'But the problem bigins here
Dim areasback As String = DataGridView1.Rows(0).Cells(6).Value.ToString
Dim areasback1 As String() = areasback.Split(",")
For Each x In areasback1
For i = 0 To areasback.Count - 1
If chklistInput.Items(i).ToString() = x.ToString() Then
chklistInput.SetItemChecked(i, False)
End If
Next
Next
End Sub
You have to loop over chklistInput.Items.Count - 1 instead of areasback.Count - 1
use the following code:
Dim areasback As String = DataGridView1.Rows(0).Cells(6).Value.ToString
Dim areasback1 As String() = areasback.Split(",")
Dim intCount as integer = 0
For each str as string in areasback1
For intCount = 0 To chklistInput.Items.Count - 1
If chklistInput.Items(intCount).ToString() = str Then
chklistInput.SetItemChecked(intCount , True)
End If
Next
Next
chklistInput.Refresh()
Note: comparing is case sensitive

Visual Basic Confusion

I have been required to create a program that asks me to find the maximum value of one particular array. I am using multiple forms in this project and have used a user-defined data type and created multiple array under it. There is a first form that is related to this, which defines my defined data type is gStudentRecord and the arrays that define it are last name, Id, and GPA. This second form is where I write all of the code to display what I want. My question is how to get the Max GPA out of that array. I'm sorry if this isn't in very good format, this is the first time I've used Stackoverflow
Public Class frmSecond
Private Sub frmSecond_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Ctr As Integer
Dim Line As String
lstDisplay.Items.Clear()
lstDisplay.Items.Add("Name".PadRight(25) & "ID".PadRight(16) & "GPA".PadRight(20) & "Out of state".PadRight(10))
For Ctr = 0 To gIndex Step 1
Line = gCourseRoster(Ctr).LastName.PadRight(20) & gCourseRoster(Ctr).ID.PadRight(15) & gCourseRoster(Ctr).GPA.ToString.PadRight(15) & gCourseRoster(Ctr).OutOfState.ToString().PadLeft(5)
lstDisplay.Items.Add(Line)
Next
End Sub
Private Sub btnStats_Click(sender As Object, e As EventArgs) Handles btnStats.Click
Dim Ctr As Integer = 0
Dim Average As Double
Dim Sum As Double
Dim Found As Boolean = False
Dim Pass As Integer
Dim Index As Integer
lstDisplay.Items.Clear()
**For Ctr = 0 To gIndex Step 1
If gCourseRoster(Ctr).GPA > gCourseRoster(Ctr).GPA Then
lstDisplay.Items.Add(gCourseRoster(Ctr).GPA)
End If
Next**
Average = gComputeAverage(Sum)
lstDisplay.Items.Add("Number of Students: " & gNumberOfStudents)
lstDisplay.Items.Add("Average: " & Average)
End Sub
Private Function gComputeAverage(Sum As Double) As Double
Dim Ctr As Integer
Dim Average As Double
For Ctr = 0 To gIndex Step 1
Sum = Sum + gCourseRoster(Ctr).GPA
Next
Average = Sum / gNumberOfStudents
Return Average
End Function
End Class
You can use a Lambda expression to tease it out. The Cast part is converting from the gCourseRoster to a collection of Double by supplying the GPA to the Select statement.
Dim gList As New List(Of gCourseRoster)
gList.Add(New gCourseRoster With {.id = 1, .name = "Bob", .GPA = 3.9})
gList.Add(New gCourseRoster With {.id = 2, .name = "Sarah", .GPA = 3.2})
gList.Add(New gCourseRoster With {.id = 3, .name = "Frank", .GPA = 3.1})
Dim maxGPA = gList.Cast(Of gCourseRoster).Select(Function(c) c.GPA).ToList.Max
MessageBox.Show(maxGPA.ToString)
Output: 3.9

populate datagridview according to selected row in another datagridview

I have two datagrid gridproperty and gridpropertyselection. There is one table which contains data. i have to papulate data in datagridviw named gridpropertyselection accoring to selected row in gridProperty datagrid.
I want to which event will be for for this. i trid the following code . but getting error in Dim index As Integer = gridProperty.CurrentRow.Index this line
Private Sub gridProperty_SelectionChanged(ByVal Sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowStateChangedEventArgs) Handles gridProperty.RowStateChanged
Dim dtPropertySelection As DataTable = m_ds.Tables(0)
Dim i As Integer
Dim count As Integer = gridPropertySelection.Rows.Count
' If (gridProperty.CurrentRow.IsNewRow IsNot Nothing) Then
Dim index As Integer = gridProperty.CurrentRow.Index
Dim PropertyID As Integer = gridProperty.Rows(index).Cells("PropertyID").Value
While i < dtPropertySelection.Rows.Count
If PropertyID = dtPropertySelection.Rows(i).Item("PropertyID") Then
gridPropertySelection.Rows.Add()
gridPropertySelection.Rows(count).Cells("colSelectionDescription").Value() = dtPropertySelection.Rows(i).Item("SelectionDescription")
gridPropertySelection.Rows(count).Cells("colAssociatedText").Value() = dtPropertySelection.Rows(i).Item("AssociatedText")
count = count + 1
End If
i = i + 1
End While
You can use the CellClick event of the datagridview.
Also, try to use
Dim index As Integer = gridProperty.CurrentCell.RowIndex()