Click here to Skip to main content
15,888,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone

I have a Listview, which when populated, looks something like this :

Always Fresh Flowers	77666666	01/06/2014	Appliance Parts           Row 1
     		                                        Insurance 	          Row 2
-----------------------------------------------------------------------------
Avolands Gauteng	66	        01/07/2014	Equipment Hire            Row 3
				                        Repairs & Maintenance     Row 4
-----------------------------------------------------------------------------
Nicoles Macaroons	6767	        01/07/2014	Gas 	                  Row 5
-----------------------------------------------------------------------------
Olive Grove	        94	        15/11/2013	C.O.S. - Food	          Row 6
		                                        C.O.S. - Beverages        Row 7
                                                        C.O.S. - Other	          Row 8
-----------------------------------------------------------------------------


Below the Listview I have a number of text boxes which are populated with the contents
of the selected row. This works fine if "Nicoles Macaroons" is selected.
If the "Insurance" row for "Always Fresh Flowers" is selected I need to place "Always Fresh Flowers", "77666666" and "01/06/2014" as well as the word "Insurance" in the Textboxes.

LisyView properties MultiSelect = False

If I click on row 4 then Focuseditem.Index returns the value 0. I need something that will return the value 4 so that I can subtract 1 from it and get SubItems from row 3 *because row 4 does not duplicate the data from the previous row in the interests of readability)

Every attempt that I have made to access one row (or more in the case of Olive Grove) above the selected row has failed.

Can anyone please suggest how I can do this.


VB
Private Sub lsv_View_All_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lsv_View_All.SelectedIndexChanged

    Dim index As Integer = lsv_View_All.FocusedItem.Index   ' Have tried a few variations of this with no luck

    If lsv_View_All.SelectedItems.Count > 0 Then
        cbo_ViewAll_Supplier.Text = lsv_View_All.SelectedItems(0).SubItems(1).Text
        txt_ViewAll_Invoice_No.Text = lsv_View_All.SelectedItems(0).SubItems(2).Text
        dtp_ViewAll_Date.Value = CDate(lsv_View_All.SelectedItems(0).SubItems(3).Text)
        cbo_ViewAll_Gl_No.Text = lsv_View_All.SelectedItems(0).SubItems(4).Text
        cbo_ViewAll_Vat.Text = lsv_View_All.SelectedItems(0).SubItems(5).Text
        txt_ViewAll_Amount.Text = lsv_View_All.SelectedItems(0).SubItems(7).Text

    End If

End Sub



Much appreciated

Darrell
Posted
Updated 9-Jan-14 2:39am
v2

try this

C#
private void listView1_Click(object sender, EventArgs e)
        {
            var list = sender as ListView;
            string SelectedItem = list.FocusedItem.Text;
            int rowNumber = list.FocusedItem.Index;
        }
 
Share this answer
 
Comments
Darrell de Wet 9-Jan-14 8:08am    
Thanks for the reply

The problem is that focuseditem.index returns the value 0.
I need to determine if the 5th row of the listview is selected how I can get something to return the value 5.
Please check the following link:
How do i get the SelectedItem or SelectedIndex of ListView in vb.net[^]
how to get the index number of the selected listview item[^]

Check the following code. And compare with your code. I think this will work
VB
Private Sub lsv_View_All_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lsv_View_All.SelectedIndexChanged

      Dim index As Integer = lsv_View_All.FocusedItem.Index   ' Have tried a few variations of this with no luck

      If lsv_View_All.SelectedItems.Count > 0 Then
          cbo_ViewAll_Supplier.Text = lsv_View_All.SelectedItems(index).SubItems(1).Text
          txt_ViewAll_Invoice_No.Text = lsv_View_All.SelectedItems(index).SubItems(2).Text
          dtp_ViewAll_Date.Value = CDate(lsv_View_All.SelectedItems(index).SubItems(3).Text)
          cbo_ViewAll_Gl_No.Text = lsv_View_All.SelectedItems(index).SubItems(4).Text
          cbo_ViewAll_Vat.Text = lsv_View_All.SelectedItems(index).SubItems(5).Text
          txt_ViewAll_Amount.Text = lsv_View_All.SelectedItems(index).SubItems(7).Text

      End If

  End Sub
 
Share this answer
 
v3
Comments
Darrell de Wet 9-Jan-14 8:07am    
I have already looked at both of those sites.
The problem is that focuseditem.index returns the value 0.
I need to determine if the 5th row of the listview is selected how I can get something to return the value 5.
Sibasisjena 9-Jan-14 8:13am    
If you are selecting multiple row then you have to for loop. Some thing like this:

For Each itm As ListViewItem In listView1.SelectedItems
If itm.Selected Then
index = itm.Index
End If
Next
Darrell de Wet 9-Jan-14 8:40am    
Perhaps I was not clear in my question so I have updated it.
Would you be kind enough to check it again.
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900