如何获取列表视图中所选项目的行号 [英] How to get row number of selected item in a listview

查看:82
本文介绍了如何获取列表视图中所选项目的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有一个Listview,当它填充时,看起来像这样:



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
-----------------------------------------------------------------------------





在Listview下面我有许多文本框,其中填充了所选行的内容

。如果选择Nicoles Macaroons,这样可以正常工作。

如果选择Always Fresh Flowers的Insurance行,我需要放置Always Fresh Flowers,77666666和01 / 06/2014以及文本框中的保险一词。



LisyView属性MultiSelect = False



如果我点击第4行,那么Focuseditem.Index返回值0.我需要返回值4的东西,这样我就可以从中减去1并从第3行获得SubItems,因为第4行不会复制数据为了便于阅读,从前一行开始)



我在所选行上方访问一行(或者更多的是橄榄树林)的所有尝试失败了。



任何人都可以建议我怎么做。







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.


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







非常感谢



Darrell




Much appreciated

Darrell

推荐答案

试试这个



try this

private void listView1_Click(object sender, EventArgs e)
        {
            var list = sender as ListView;
            string SelectedItem = list.FocusedItem.Text;
            int rowNumber = list.FocusedItem.Index;
        }


请检查以下链接:

如何在vb.net中获取ListView的SelectedItem或SelectedIndex [ ^ ]

如何获取所选列表视图项的索引号 [ ^ ]



查看以下代码。并与您的代码进行比较。我认为这将有效

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
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


这篇关于如何获取列表视图中所选项目的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆