How to access a cell of an ObjectListView

BenTam-3003 686 Reputation points
2022-04-29T02:29:07.15+00:00

Dear All,

How to access a cell of an ObjectListView (Not DataGridView) by row and column?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2022-04-29T06:51:50.95+00:00

    @BenTam-3003 , Welcome to Microsoft Q&A, you could try the following code to access the cell of an ObjectListView by row index and column index.

    private void Form1_Load(object sender, EventArgs e)  
            {  
                objectListView1.Columns.Add(new OLVColumn("Name","Name"));  
                objectListView1.Columns.Add(new OLVColumn("Id", "Id"));  
                objectListView1.Columns.Add(new OLVColumn("Age", "Age"));  
                Student student1 = new Student() { Name="test1", Id=1003, Age=25};  
                Student student2 = new Student() { Name = "test2", Id = 1002, Age = 20 };  
                Student student3 = new Student() { Name = "test3", Id = 1001, Age = 21 };  
                List<Student> students = new List<Student>() { student1, student2, student3 };  
                //objectListView1.View = View.Details;  
                objectListView1.SetObjects(students);  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                string value = GetResult(0, 2);  
                  
                textBox1.Text= value;  
            }  
      
            public string GetResult(int row,int col)  
            {  
                List<object> list = new List<object>();  
                list = objectListView1.Objects.Cast<object>().ToList();  
                string value = objectListView1.GetColumn(col).GetStringValue(list[row]);  
                return value;  
            }  
    

    Result:

    197567-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful