Unable to select values in second column listview - vb.net

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

Related

Iterate and Update same Collection in VBA

My requirement is to iterate through a VBA collection , and based on certain
condition update the subsequent records in the same collection.
For example lets say "TMasterList" is a collection which has total of 4 records ,
while 2nd record gets processed in the below loop,
I need to update 3rd and 4th record and then continue with the loop(ie process 3 and 4th).
For Each objEachTmapping In TMasterList
Next
Any suggestions on how to implement it.
Loop over the index, checking the bounds if needed:
Dim i As Long, max As Long
max = TMasterList.Count
For i = 1 To max
TMasterList.Item(i).value = Foo
If i + 1 < max Then TMasterList.Item(i + 1).value = Bar
If i + 2 < max Then TMasterList.Item(i + 2).value = Qux
Next
This assumes that there is a reference in the collection, if its a type it cannot be modified in situ.

Find and sum value of a datagridview column in vb.net

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

Colouring datagrid rows

I was wondering how to colour the first 8 rows of a datagridview. I have managed to sort the values in descending order and I wish to have the first 8 rows coloured to highlight the top 8 to the user, and I'm not sure how to go about doing this.
Dim count As Integer
For count = 0 To datagridsort.RowCount - 1
Do
datagridsort.Rows(0).Cells(0).Style.BackColor = Color.Coral
datagridsort.Rows(0).Cells(1).Style.BackColor = Color.Coral
Loop Until count = 8
Next
In the code you posted in your comment, you were never using the count variable. You were only updated the first row every time. Try it like this:
For i As Integer = 0 To Math.Min(datagridsort.RowCount - 1, 7)
For j As Integer = 0 To datagridsort.ColumnCount - 1
datagridsort.Rows(i).Cells(j).Style.BackColor = Color.Coral
Next
Next

how to count specific word in a listview column

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!

How to get the value on a specific row by searching?

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.