ObjectListView - Retrieve cell value by column name

37 Views Asked by At

I have an ObjectListView grid similar to the one below. For understanding purpose of this query, I have shown the column name in brackets:

Id (olvColumn1) Unique Id (olvColumn2) Parent Id (olvColumn3) Name (olvColumn4) Desc (olvColumn5)
1003 1 7 Name 1 Test desc 1
1003 3 2 Name 2 Test desc 2
1003 4 5 Name 3 Test desc 3
1004 2 6 Name 4 Test desc 4
1004 5 9 Name 5 Test desc 5

I wish to extract the value if a cell in "Unique Id" column using the column name "olvColumn2".

There is no selected row, so please do not suggest the SelectedRows method of the grid. I wish to iterate through the entire grid row wise, pickup up the column value in "Unique Id" column. I have achieved this with the below code, but the code below uses index value of the column and not the column name. The problem with index value is that if the columns are reordered either during design time of run-time, my code would start picking up the wrong column and wrong resultant cell value. My understanding is that with the Column Name, this problem can be avoided. Below is the sample code.

I came across partly similar query but it's related to DataGridView and not ObjectListview.

Please afvise:

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
1

There are 1 best solutions below

0
Rev On

You can just iterate through the underlying model objects.

With ObjectListView, as soon as you find yourself using ListViewItems, you are 99% doing something wrong.