2016-11-10 83 views
0

获取数据我已经创建了一个列表视图,并使用数据表中填充了..
所有工作得很好...
现在我添加复选框,所有项目.. 现在我想喜欢当最后一列的值的项目是“真”,那么检查相同项目的复选框。的ListView:从选定项目

我想下面的代码...

If LstViewHelp.Items.Count <> 0 Then 
    For Each item As ListViewItem In LstViewHelp.Items 
     If LstViewHelp.FocusedItem.SubItems(10).Text = "True" Then 
      LstViewHelp.FocusedItem.Checked = True 
     End If 
    Next 
End If 

我收到以下错误对象引用未设置为一个实例。
尝试了很多链接,但没有找到适当的解决方案...!

+1

使用'DataGridView'对于这样的要求会好得多。您可以简单地将数据表分配给'DataGridView'的'DataSource'并显示'CheckBox'作为布尔列。 –

+0

如果您想对所有项目执行操作,那么您的迭代器变量是'item'而不是'FocusedItem'。一个'DataGridView'会更合适 – Plutonix

+0

实际上我重复使用listview,我创建了很多东西,比如搜索记录和所有这个表单,并且使用了这个不同的表单......它就像其他表单的帮助表单...我读了互联网和一些建议datagridview但问题是我必须编写DataGridView相同的长代码...在ListView中的帮助将是我现在更好的选择.... @RezaAghaei – bonny

回答

0

您可以使用SelectedIndices获得所有选定索引,如ListView.SelectedIndexCollection。在选定索引上添加ForEach循环并编辑您的子项目。

Dim indexes As ListView.SelectedIndexCollection = Me.ListViewHelp.SelectedIndices 

For Each index In indexes 
    If Me.ListViewHelp.Items(index).SubItems(10).Text = "True" Then 
     LstViewHelp.Items(index).Checked = True 
    End If 

Next 

如果你要检查所有项目,你可以使用for循环

For i = 0 To Me.ListViewHelp.Items.Count - 1 

      If Me.ListViewHelp.Items(i).SubItems(10).Text = "True" Then 
      LstViewHelp.Items(i).Checked = True 
      End If 
Next 
+0

好的,触发器没有进入循环..我有超过100个记录在列表视图。 – bonny

+0

100选定的记录?索引是选定索引的集合。你想在所有的listview上做到这一点? –

+0

没有选择的记录,只是总项目是100大约10列的每个项目...我的multiselect是假的,fullrowselect是真的 – bonny