我有一个类似于下面的 ObjectListView 网格。为了理解此查询的目的,我在括号中显示了列名称:
Id(olvColumn1) | 唯一 ID (olvColumn2) | 父 ID(olvColumn3) | 名称(olvColumn4) | 描述(olvColumn5) |
---|---|---|---|---|
1003 | 1 | 7 | 姓名1 | 测试说明1 |
1003 | 3 | 2 | 姓名2 | 测试说明2 |
1003 | 4 | 5 | 姓名3 | 测试说明3 |
1004 | 2 | 6 | 名字4 | 测试说明4 |
1004 | 5 | 9 | 名字5 | 测试说明5 |
我希望使用列名称“olvColumn2”提取“Unique Id”列中的单元格的值。
没有选中的行,所以请不要建议网格的SelectedRows方法。我希望逐行迭代整个网格,获取“Unique Id”列中的列值。我已经使用下面的代码实现了这一点,但是下面的代码使用列的索引值而不是列名称。索引值的问题是,如果在设计时或运行时期间对列重新排序,我的代码将开始选取错误的列和错误的结果单元格值。我的理解是,有了Column Name,这个问题就可以避免。以下是示例代码。
我遇到了部分类似的query,但它与DataGridView相关,而不是ObjectListview。
请告知:
Private Function GetRowNextUniqueId() As Integer
Dim I As Integer
Dim mytmpRowId As Integer
Dim myUniqueRowId As Integer
For Each itm As ListViewItem In dlvEstimate.Items
mytmpRowId = Convert.ToInt32(itm.SubItems(0).Text) ' Here itm.SubItems(0) does not allow me to pass the column name. I am looking for a column where I can pass the column name instead of the column index.
If mytmpRowId > myUniqueRowId Then
myUniqueRowId = mytmpRowId
End If
Next
Return myUniqueRowId
End Function
您只需迭代底层模型对象即可。
使用 ObjectListView,一旦你发现自己使用 ListViewItems,你 99% 就做错了。