I have a form which is 1 list view and a text box.
* list view has a total of 100 rows of data
* list view has 5 columns
* column 3 has only two possible words yes or no
I want to count the number of occurrence of the word yes in column 3
the total row can be count with this code:
''''''''''COUNT TOTAL ADMISSION''''''''''''''
Dim rowcount As Integer = 0
For Each item As ListViewItem In LVfeestatementBA_I.Items
rowcount = CInt(item.SubItems(0).Text) 'Considering every column always have some value
Next
txttotaladBA_I.Text = rowcount.ToString()
any help will be greatfull
EDIT 1
This is a school assignment. As I said my aim is to find out the number of occurrence of a word in column 3. I have database of MS access which is connected with code and provides the data for the list view. The list view has 5 columns and there are a total of 100 rows. The data in col-3 contains only three words gen, occ, and cc. Now in want to count col-3 for the words with code and show the number like (68) in textbox1
EDIT 2
I applied the function provided by thedarkspoon, but it's not showing the result. I just want the result to be shown in textbox1, ex. if total number of words are 78 then at the time of form_load it should show 78 in textbox1. I solved the problem by adding at last textbox1.text = numofyes and change variable from integer to string now its working
I did not quite understand your scenario (you have to be more clear).
Anyway, given a ListView that displays items that each have 3 subitems and we know that the third subitem will have values of either "yes" or "no" we can build a query like (using linq):
var collectionOfListViewItems = listView1.Items.Cast<ListViewItem>();
var numberOfrowsWithTheThirdSubItemTextEqualToYes = (from c in collectionOfListViewItems where c.SubItems[3].Text == "yes" select c).Count();
Without linq you could do a foreach:
var numberOfrowsWithTheThirdSubItemTextEqualToYes = 0;
foreach (ListViewItem item in listView1.Items)
{
if (item.SubItems[3].Text == "yes")
numberOfrowsWithTheThirdSubItemTextEqualToYes++;
}
Ok here you go, I made this a function but you could easily adapt this to a subroutine:
Function countyes()
'Set up a variable to count the number of yes:
Dim numofyes As Integer = 0
'Count the number of yes (Replace listview1 with the name of your listview):
For Each item As ListViewItem In ListView1.Items
'If the Yes/No is in column 3, you are looking in subitem 2:
If item.SubItems(2).Text = "Yes" Then
'Increment the variable by one if the value is yes:
numofyes = numofyes + 1
End If
Next
'Return our total number of Yes that we found:
Return numofyes
End Function
Hope this helps!
Related
I created a DataGridView where I am retrieving data by search customer id. I want to sum values in TextBox when I select some specific CheckBox-es of DataGridView.
But the problem is that it is adding all values in all cells of all rows but it should be like that for example; if Column number is 2, then pending value should be shown once in TextBox not after sum both duplicated received cell, and if Column numbers are different like Column number 1 and Column number 2 and both are selected, sum value should be there in TextBox which is named received from data grid view received cell.
I am attaching image and code also what I am trying to do. can someone guide how to do this?
Private Sub Getvalue()
For Each row As DataGridViewRow In DataGridView1.Rows
If DataGridView1.RowCount > 0 Then
Dim isSelected As Boolean = Convert.ToBoolean(row.Cells(0).Value)
If isSelected Then
Dim pending As String = 0
Dim received As String = 0
Dim final amount As String = 0
For index As Integer = 0 To DataGridView1.RowCount - 1
pending += Convert.ToDouble (DataGridView1.Rows(index).Cells(15).Value)
received += Convert.ToDouble(DataGridView1.Rows(index).Cells(14).Value)
finalamount += Convert.ToDouble(DataGridView1.Rows(index).Cells(12).Value)
Next
Label19.Reset Text()
Label19.Text = pending
Label20.Reset Text()
Label20.Text = received
Label24.Reset Text()
Label24.Text = final amount
If Label19.Text <= 0 Then
Label18.Text = ""
Else
Label18.Text = Val(Label 24.Text - Label 19.Text)
End If
End If
End If
Next
End Sub
First, turn on Option Strict as per my comment.
If there are not rows, your For Each row won't run so there is no need to check the RowCount.
Don't declare your accumulation variables inside the loop. They will be a new variable on each iteration. Your inner loop loops through all rows so, of course, you get the sum of all. Eliminate the inner loop.
I have no idea what Reset Text() is. A method can't have a space in its name. Anyway, it you are adding a new value it isn't necessary to clear the old one. If you do need to clear then set to "". Don't update your labels until after the loop. It is useless to update the user interface on each iteration. It will go by too fast for the user to see and it slows down the code.
In general, you need to do arithmetic or numeric comparisons with numeric types and set .Text properties with String type. The Val method has bee replace with several .net methods. .Parse, .TryParse, Convert. and in vb the CInt, CDec, etc.
Private Sub Getvalue()
Dim pending As Double = 0
Dim received As Double = 0
Dim finalAmount As Double = 0
For Each row As DataGridViewRow In DataGridView1.Rows
Dim isSelected As Boolean = CBool(row.Cells(0).Value)
If isSelected Then
pending += CDbl(row.Cells(15).Value)
received += CDbl(row.Cells(14).Value)
finalAmount += CDbl(row.Cells(12).Value)
End If
Next
Label19.Text = pending.ToString
Label20.Text = received.ToString
Label24.Text = finalAmount.ToString
If pending <= 0 Then
Label18.Text = ""
Else
Label18.Text = (finalAmount - pending).ToString
End If
End Sub
EDIT
Normally you would have a purchase. The purchase would have a table in the database with fields like purchase number, date, customer, etc. You would have another table with purchase details. The details table would have fields like details number (primary key), a foreign key purchase number which must match an entry in the purchase table, an item ID and a price.
When a customer wants to return an item or items from a purchase. You would enter the purchase number and fill your grid from the purchase details table.
Select * From PurchaseDetails Where purchaseNumber = #purchaseNumber
Put the check mark by each item returned and sum up the price column.
Purchase Table
PurchaseID | CustomerID | Date
Purchase Details Table
DetailID | PurchaseID | ItemCode | ItemPrice
I want to find and sum qty value of searched id in data grid view column am using this code
Dim tqty As Double
For Each row As DataGridViewRow In dgv.Rows
If row.Cells.Item(0).Value = cmbItemCode.Text Then
tqty += row.Cells.Item(4).Value
Textbox1.text=tqty
Exit For
End If
Next
The problem is that Textbox1 shows only one top searched row value. For example
id item name qty
1 abc 4
2 xyz 10
1 abc 10
Textbox1 shows the Result only 4.
As soon as you hit the first value, you exit the for statement. Therefore you never get pass the first value. Erase the Exit For it should work.
If DataGridView2.RowCount > 1 Then
Dim tqty As Integer = 0
'if you have the other column to get the result you could add a new one like these above
For index As Integer = 0 To DataGridView2.RowCount - 1
amount += Convert.ToInt32(DataGridView2.Rows(index).Cells(2).Value)
'if you have the other column to get the result you could add a new one like these above (just change Cells(2) to the one you added)
Next
TextBox1.Text = tqty
I've a listview that dynamically populates values between two columns in this fashion:
column 1 | column 2
value1 | value 2
value3 | value 4
however I'm unable to select any values in column 2 - is there a property of the listview that's preventing me from doing this or is it the way I populate these columns? Here's my code to populate the column:
For k = 0 To UBound(tempValues)
Dim itm As New ListViewItem(tempValues(k))
If k Mod 2 = 0 Then
listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
itm.SubItems.Add(tempValues(k + 1))
listview1.Items.Add(itm)
End If
listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
Next
Any ideas?
The closest I can see for this is to set listView1.FullRowSelect = true (I assume you have listView1.View = View.Details?)
This however will only give you full row selecting - Remember the 2nd column represents the 1st Sub Item of the listview's items.
If you want multiple columns of data, you might be better off setting listView1.View = View.Details = View.List, which will cause it to wrap a single list of items onto multiple columns when it runs out of vertical space.
Edit:
If you use listView1.View = View.List, your population would need to change to the following:
For k = 0 To UBound(tempValues)
listview1.Items.Add(new ListViewItem(tempValues(k))
Next
listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
But it would mean you end up with the list like so:
Value 1
Value 2
Value 3
Value 4
And if ListView was made too short to display, all these, it would wrap them:
Value 1 Value 4
Value 2
Value 3
How to get the value on a specific row by searching?
i.e., I have a gridview:
A B C
---------
1 2 3
2 3 4
3 4 5
Then, I want to get the "3" value from column 'A'. But I do not know the exactly row number because it can be randomly show. Fyi, in order to get the "3" value, you have to compare it to a variable. So, if the variable contains '2', then I should find the '2' value on the gridview.
How can I do that? Thank you very much..
You can check by looping the rows of gridview, see the below code
Dim num As Integer = 2
For Each DRow As DataGridViewRow In DataGridView1.Rows
If (DRow.Cells(0).Value.ToString = num.ToString) Then
MessageBox.Show("Matched")
End If
Next
Note : The above code is a sample written on datagridview, you change the code to a gridview and check.
I am trying to write a code that will display a value depending on what checkbox is selected. There are a total of 5 checkboxes and I will be adding additional checkboxes in the future so I was wondering if there is an easy way to determine which checkboxes are checked to determine which values to display. I can do this in a really round about way but I would like minimize my code if possible.
In other words, if i write each scenario out I would have to write a separate code for all of the different selection possbilities:
1 only,2 only,3 only,4 only,5 only
1+2, 1+3, 1+4, 1+5, 2+3, 2+4, 2+5, 3+4, 3+5, 4+5
1+2+3, 1+2+4,1+2+5, 1+3+4,1+3+5, 1+4+5,2+3+4, 2+3+5,3+4+5
1+2+3+4, 1+2+3+5, 1+3+4+5, 2+3+4+5
1+2+3+4+5
Each value is associated with a sub that will fill the array if it is selected. And after the arrays are filled I need to perform an additional function on the ones that are selected. The function performed is the same but I do not want to perform the function if a value is not selected because it will defeat the purpose of my function otherwise. The function itself is to select duplicates from the arrays that were selected into another array.
You can use binary numbers for each checkbox:
First value is 1 (=20)
Second value is 2 (=21)
Third value is 4 (=22)
Fourth value is 8 (=22)
Fifth value is 16 (=24)
Hence, when you sum each permutation, you have a unique number you can check.
1 only is 0, 2 only is 1 and so on
1+2 is 3, 1+3 is 5, 1+4 is 9, 1+5 is 17
and so on
You can create arrays with every case you want to check.
At least, i hope this will give you some ideas or tips.
Regards,
What #JMax is referring to is more commonly known as bit-masking. Here's a quick tutorial:
Create a sample form named Form1 with 5 checkboxes named Check1, Check2, Check3, Check4, Check5. Then add the following two functions in a standard code module:
Function GetSelectedBoxes() As Long
Dim Ctl As Control, Total As Long
For Each Ctl In Forms!form1
If Ctl.ControlType = acCheckBox Then
If Ctl Then
Total = Total + 2 ^ CLng(Mid(Ctl.Name, 6))
End If
End If
Next Ctl
GetSelectedBoxes = Total
End Function
Sub ShowSelectedBoxes()
Dim Total As Long, i As Integer
Total = GetSelectedBoxes
For i = 1 To 5
If Total And 2 ^ i Then Debug.Print "Check" & i & " selected"
Next i
End Sub
Now open Form1 and check boxes 1, 3, and 4. Run the ShowSelectedBoxes routine and you should get the following output in the immediate window:
Check1 selected
Check3 selected
Check4 selected
I used the For...Loop for compactness in my sample, but you can just as easily break it out into separate If...Then statements to do something more meaningful with the checked boxes.
This approach will support up to 31 or 32 separate checkboxes.