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.
Related
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 have a large datasheet, where each row has a column that gives it a unique identifier, like so:
Row Identifier Value
1 A 3
2 A 4
3 A 7
4 B 1
5 B 3
6 B 0
7 C 9
I want to be able to find the row number of the last duplicated identifier in a range. So, for example, if the identifier "B" was some unknown number of rows long, and I wanted to find the number of the last row that had the column identifier "B," how would I do so?
So far I've come up with code to select the row of the FIRST identifier:
' Select first row where first instance of <B> exists:
With ActiveWorkbook.ActiveSheet
Set First_B_Row = .Range("A:A").Find(What:="B", LookIn:=xlValues)
End With
Dim First_B_RowNumber As Long
First_B_RowNumber = First_B_Row.Row
ActiveWorkbook.ActiveSheet.Rows(First_B_RowNumber).Select
But, I want to be able to find the row number of the LAST identifier in a column full of them. Any ideas?
You are absolutely close. Instead of searching top to bottom, then search in reverse like this:
With ActiveWorkbook.ActiveSheet
Dim Last_B_RowNumber As Long
Last_B_RowNumber = .Range("A:A").Find(What:="B", After:=[A1], _
SearchDirection:=xlPrevious).Row
End With
Above code will find the last occurrence of B in Column A and assigns it's row number to a variable.
Your just missing the SearchDirection argument.
I was looking into how to create a shortcut with the help of VBA. The macro would basically allow me to filter the range based on cell's value. I found this macro on the internet
Public Sub FilterOnCellValue()
Dim nField As Long
With ActiveCell
nField = .Column - .CurrentRegion.Cells(1).Column + 1
.CurrentRegion.AutoFilter Field:=nField, Criteria1:=.Value
End With
End Sub
The code works fine, but I have a hard time trying to understant how the dude create. So what I know so far is the guys is creating a variable. but for some reason the variable is not in a string format but in "long" and the code works perfectly when I filter based on text in a cell. I thought that long can only be used for numbers, correct me if I am wrong (which i think I am:)). Secondly. whats up with .column - .currentregion. cells.....+1? I real have no idea what he did there.
Would really appreciate some help here. the forum has been very useful to me so far.
The secret to understanding anything is to break it into parts
Anyways, the documentation of Autofilter states that
Public Sub FilterOnCellValue()
Dim nField As Long
With ActiveCell
'.Column: Column Number i.e A=1,B=2 etc
'.CurrentRegion.Cells(1).Column: Gets the col No of the firstcell of the data region
nField = .Column - .CurrentRegion.Cells(1).Column+1
'Autofilter takes column number as the parameter
'see example below
'nField: gets the col number of the filter value within the region
' leftmost field is 1 hence +1
.CurrentRegion.AutoFilter Field:=nField, Criteria1:=.Value
End With
End Sub
for e.g if your data is in this format
A B
1 ID Value
2 1 Apple
3 2 Orange
4 3 Banana
5 4 Apple
6 5 Banana
Lets say you click on cell B5(Apple) and run the macro then
The macro first gets the column number of the selected cell =2
checks which column the current region's (A1:B6) first cell(ID) is in =1
calculate value in Step 1-Step 2 to get which column the selected value lies in, with respect to the region (A1:B6) , the idea is using the .column as reference to find out the column number of selected value within the region result of the step:2-1+1=2
Filters the column number passed from step 3(in our example value is 2) of the region A1:B6 for the selected value(Apple)
Hope this helps
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
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!